From 500da47fe5c2c822f2ab55c8009811cf1386e76a Mon Sep 17 00:00:00 2001 From: "Eon S. Jeon" Date: Wed, 15 May 2019 06:56:34 +0900 Subject: [PATCH] [node] add a new option, withFileTypes, to fs.promises.readdir (#35213) * [node] add withFileTypes option to fs.promises.readdir node v12 newly added withFileTypes option to fs.promises.readdir * [node] add a test for fs.promises.readdir --- types/node/fs.d.ts | 13 ++++++++++--- types/node/node-tests.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) 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 }); + } } ////////////////////////////////////////////////////