diff --git a/types/node/fs.d.ts b/types/node/fs.d.ts index bb100fc831..b51679c876 100644 --- a/types/node/fs.d.ts +++ b/types/node/fs.d.ts @@ -858,7 +858,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. */ - function readdir(path: PathLike, options: { withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void; + function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace readdir { @@ -888,7 +888,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent */ - function __promisify__(path: PathLike, options: { withFileTypes: true }): Promise; + function __promisify__(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise; } /** @@ -913,11 +913,11 @@ declare module "fs" { function readdirSync(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): string[] | Buffer[]; /** - * Asynchronous readdir(3) - read a directory. + * Synchronous readdir(3) - read a directory. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. */ - function readdirSync(path: PathLike, options: { withFileTypes: true }): Dirent[]; + function readdirSync(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Dirent[]; /** * Asynchronous close(2) - close a file descriptor. diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index d5fe429815..2fb2ab24c8 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -109,6 +109,7 @@ import Module = require("module"); listS = fs.readdirSync('path', undefined); const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true }); const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true }); let listB: Buffer[]; listB = fs.readdirSync('path', { encoding: 'buffer' }); @@ -120,6 +121,23 @@ import Module = require("module"); fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException | null, files: fs.Dirent[]) => {}); } + + async function testPromisify() { + const rd = util.promisify(fs.readdir); + let listS: string[]; + listS = await rd('path'); + listS = await rd('path', 'utf8'); + listS = await rd('path', null); + listS = await rd('path', undefined); + listS = await rd('path', { encoding: 'utf8' }); + listS = await rd('path', { encoding: null }); + listS = await rd('path', { encoding: null, withFileTypes: false }); + listS = await rd('path', { encoding: 'utf8', withFileTypes: false }); + const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true }); + const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true }); + } + { fs.mkdtemp('/tmp/foo-', (err, folder) => { console.log(folder); diff --git a/types/node/v10/fs.d.ts b/types/node/v10/fs.d.ts index 90db179550..ee94a8af86 100644 --- a/types/node/v10/fs.d.ts +++ b/types/node/v10/fs.d.ts @@ -858,7 +858,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. */ - function readdir(path: PathLike, options: { withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void; + function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace readdir { @@ -888,7 +888,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent */ - function __promisify__(path: PathLike, options: { withFileTypes: true }): Promise; + function __promisify__(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise; } /** @@ -913,11 +913,11 @@ declare module "fs" { function readdirSync(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): string[] | Buffer[]; /** - * Asynchronous readdir(3) - read a directory. + * Synchronous readdir(3) - read a directory. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. */ - function readdirSync(path: PathLike, options: { withFileTypes: true }): Dirent[]; + function readdirSync(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Dirent[]; /** * Asynchronous close(2) - close a file descriptor. diff --git a/types/node/v10/node-tests.ts b/types/node/v10/node-tests.ts index 417689bc88..6c24482076 100644 --- a/types/node/v10/node-tests.ts +++ b/types/node/v10/node-tests.ts @@ -261,6 +261,7 @@ import { Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer, transcode, listS = fs.readdirSync('path', undefined); const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true }); const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true }); let listB: Buffer[]; listB = fs.readdirSync('path', { encoding: 'buffer' }); @@ -272,6 +273,23 @@ import { Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer, transcode, fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException, files: fs.Dirent[]) => {}); } + + async function testPromisify() { + const rd = util.promisify(fs.readdir); + let listS: string[]; + listS = await rd('path'); + listS = await rd('path', 'utf8'); + listS = await rd('path', null); + listS = await rd('path', undefined); + listS = await rd('path', { encoding: 'utf8' }); + listS = await rd('path', { encoding: null }); + listS = await rd('path', { encoding: null, withFileTypes: false }); + listS = await rd('path', { encoding: 'utf8', withFileTypes: false }); + const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true }); + const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true }); + } + { fs.mkdtemp('/tmp/foo-', (err, folder) => { console.log(folder); diff --git a/types/node/v11/fs.d.ts b/types/node/v11/fs.d.ts index bb100fc831..b51679c876 100644 --- a/types/node/v11/fs.d.ts +++ b/types/node/v11/fs.d.ts @@ -858,7 +858,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. */ - function readdir(path: PathLike, options: { withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void; + function readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }, callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace readdir { @@ -888,7 +888,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent */ - function __promisify__(path: PathLike, options: { withFileTypes: true }): Promise; + function __promisify__(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise; } /** @@ -913,11 +913,11 @@ declare module "fs" { function readdirSync(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): string[] | Buffer[]; /** - * Asynchronous readdir(3) - read a directory. + * Synchronous readdir(3) - read a directory. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent. */ - function readdirSync(path: PathLike, options: { withFileTypes: true }): Dirent[]; + function readdirSync(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Dirent[]; /** * Asynchronous close(2) - close a file descriptor. diff --git a/types/node/v11/node-tests.ts b/types/node/v11/node-tests.ts index d5fe429815..2fb2ab24c8 100644 --- a/types/node/v11/node-tests.ts +++ b/types/node/v11/node-tests.ts @@ -109,6 +109,7 @@ import Module = require("module"); listS = fs.readdirSync('path', undefined); const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true }); const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true }); let listB: Buffer[]; listB = fs.readdirSync('path', { encoding: 'buffer' }); @@ -120,6 +121,23 @@ import Module = require("module"); fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException | null, files: fs.Dirent[]) => {}); } + + async function testPromisify() { + const rd = util.promisify(fs.readdir); + let listS: string[]; + listS = await rd('path'); + listS = await rd('path', 'utf8'); + listS = await rd('path', null); + listS = await rd('path', undefined); + listS = await rd('path', { encoding: 'utf8' }); + listS = await rd('path', { encoding: null }); + listS = await rd('path', { encoding: null, withFileTypes: false }); + listS = await rd('path', { encoding: 'utf8', withFileTypes: false }); + const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true }); + const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true }); + } + { fs.mkdtemp('/tmp/foo-', (err, folder) => { console.log(folder);