diff --git a/types/node/fs.d.ts b/types/node/fs.d.ts index 88eaef6b59..bb100fc831 100644 --- a/types/node/fs.d.ts +++ b/types/node/fs.d.ts @@ -2056,21 +2056,28 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. */ - function readdir(path: PathLike, options?: { encoding?: BufferEncoding | null } | BufferEncoding | null): Promise; + function readdir(path: PathLike, options?: { encoding?: BufferEncoding | null; withFileTypes?: false } | BufferEncoding | null): Promise; /** * Asynchronous 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 The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. */ - function readdir(path: PathLike, options: { encoding: "buffer" } | "buffer"): Promise; + function readdir(path: PathLike, options: { encoding: "buffer"; withFileTypes?: false } | "buffer"): Promise; /** * Asynchronous 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 The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used. */ - function readdir(path: PathLike, options?: { encoding?: string | null } | string | null): Promise; + function readdir(path: PathLike, options?: { encoding?: string | null; withFileTypes?: false } | string | null): Promise; + + /** + * Asynchronous 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 readdir(path: PathLike, options: { encoding?: string | null; withFileTypes: true }): Promise; /** * Asynchronous readlink(2) - read value of a symbolic link. diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 06ba08771f..69981a7722 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -278,6 +278,18 @@ import Module = require("module"); mode: 0o777, }); } + + { + let names: Promise; + let buffers: Promise; + let namesOrBuffers: Promise; + let entries: Promise; + + names = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: false }); + buffers = fs.promises.readdir('/path/to/dir', { encoding: 'buffer', withFileTypes: false }); + namesOrBuffers = fs.promises.readdir('/path/to/dir', { encoding: 'SOME OTHER', withFileTypes: false }); + entries = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: true }); + } } ////////////////////////////////////////////////////