Skip to content

fix: types #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resolve.sync("/some/path/to/folder", "../../dir");

const myResolve = resolve.create({
// or resolve.create.sync
extensions: [".ts", ".js"]
extensions: [".ts", ".js"],
// see more options below
});

Expand All @@ -63,7 +63,7 @@ const { CachedInputFileSystem, ResolverFactory } = require("enhanced-resolve");
const myResolver = ResolverFactory.createResolver({
// Typical usage will consume the `fs` + `CachedInputFileSystem`, which wraps Node.js `fs` to add caching.
fileSystem: new CachedInputFileSystem(fs, 4000),
extensions: [".js", ".json"]
extensions: [".js", ".json"],
/* any other resolver options here. Options/defaults can be seen below */
});

Expand All @@ -79,7 +79,7 @@ myResolver.resolve(
resolveContext,
(err /* Error */, filepath /* string */) => {
// Do something with the path
}
},
);
```

Expand Down
7 changes: 7 additions & 0 deletions generate-types-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

module.exports = {
typeMapping: {
"^signal in Abortable events Interface": "AbortSignal",
},
};
43 changes: 23 additions & 20 deletions lib/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ const {

/**
* @typedef {object} ObjectEncodingOptions
* @property {(BufferEncoding | null | undefined)=} encoding encoding
* @property {BufferEncoding | null | undefined=} encoding encoding
*/

/**
* @typedef {ObjectEncodingOptions | BufferEncoding | undefined | null} EncodingOption
*/

/** @typedef {(err: NodeJS.ErrnoException | null, result?: string) => void} StringCallback */
Expand All @@ -56,10 +60,6 @@ const {
/** @typedef {(err: NodeJS.ErrnoException | null, result?: IBigIntStats) => void} BigIntStatsCallback */
/** @typedef {(err: NodeJS.ErrnoException | null, result?: (IStats | IBigIntStats)) => void} StatsOrBigIntStatsCallback */
/** @typedef {(err: NodeJS.ErrnoException | Error | null, result?: JsonObject) => void} ReadJsonCallback */
/** @typedef {(err: NodeJS.ErrnoException | null, result?: string[]) => void} ReaddirStringCallback */
/** @typedef {(err: NodeJS.ErrnoException | null, result?: Buffer[]) => void} ReaddirBufferCallback */
/** @typedef {(err: NodeJS.ErrnoException | null, result?: (string[] | Buffer[])) => void} ReaddirStringOrBufferCallback */
/** @typedef {(err: NodeJS.ErrnoException | null, result?: Dirent[]) => void} ReaddirDirentCallback */

/**
* @template T
Expand Down Expand Up @@ -96,10 +96,12 @@ const {
*/

/**
* @typedef {IStatsBase<bigint> & { atimeNs: bigint, mtimeNs: bigint, ctimeNs: bigint, birthtimeNs: bigint }} IBigIntStats
* @typedef {IStatsBase<bigint> & { atimeNs: bigint, mtimeNs: bigint, ctimeNs: bigint, birthtimeNs: bigint }} IBigIntStats
*/

/* eslint-disable jsdoc/require-template */
/**
* @template {string | Buffer} [T=string]
* @typedef {object} Dirent
* @property {() => boolean} isFile true when is file, otherwise false
* @property {() => boolean} isDirectory true when is directory, otherwise false
Expand All @@ -108,9 +110,11 @@ const {
* @property {() => boolean} isSymbolicLink true when is symbolic link, otherwise false
* @property {() => boolean} isFIFO true when is FIFO, otherwise false
* @property {() => boolean} isSocket true when is socket, otherwise false
* @property {string} name name
* @property {string} path path
* @property {T} name name
* @property {string} parentPath path
* @property {string=} path path
*/
/* eslint-enable jsdoc/require-template */

/**
* @typedef {object} StatOptions
Expand All @@ -132,10 +136,6 @@ const {
* }} ReadFile
*/

/**
* @typedef {ObjectEncodingOptions | BufferEncoding | undefined | null} EncodingOption
*/

/**
* @typedef {'buffer'| { encoding: 'buffer' }} BufferEncodingOption
*/
Expand All @@ -150,23 +150,26 @@ const {

/**
* @typedef {{
* (path: PathLike, options: { encoding: BufferEncoding | null, withFileTypes?: false | undefined, recursive?: boolean | undefined } | BufferEncoding | undefined | null, callback: ReaddirStringCallback): void;
* (path: PathLike, options: { encoding: 'buffer', withFileTypes?: false | undefined, recursive?: boolean | undefined } | 'buffer', callback: ReaddirBufferCallback): void;
* (path: PathLike, callback: ReaddirStringCallback): void;
* (path: PathLike, options: (ObjectEncodingOptions & { withFileTypes?: false | undefined, recursive?: boolean | undefined }) | BufferEncoding | undefined | null, callback: ReaddirStringOrBufferCallback): void;
* (path: PathLike, options: ObjectEncodingOptions & { withFileTypes: true, recursive?: boolean | undefined }, callback: ReaddirDirentCallback): void;
* (path: PathLike, options: { encoding: BufferEncoding | null, withFileTypes?: false | undefined, recursive?: boolean | undefined } | BufferEncoding | undefined | null, callback: (err: NodeJS.ErrnoException | null, files?: string[]) => void): void;
* (path: PathLike, options: { encoding: 'buffer', withFileTypes?: false | undefined, recursive?: boolean | undefined } | 'buffer', callback: (err: NodeJS.ErrnoException | null, files?: Buffer[]) => void): void;
* (path: PathLike, options: (ObjectEncodingOptions & { withFileTypes?: false | undefined, recursive?: boolean | undefined }) | BufferEncoding | undefined | null, callback: (err: NodeJS.ErrnoException | null, files?: string[] | Buffer[]) => void): void;
* (path: PathLike, callback: (err: NodeJS.ErrnoException | null, files?: string[]) => void): void;
* (path: PathLike, options: ObjectEncodingOptions & { withFileTypes: true, recursive?: boolean | undefined }, callback: (err: NodeJS.ErrnoException | null, files?: Dirent<string>[]) => void): void;
* (path: PathLike, options: { encoding: 'buffer', withFileTypes: true, recursive?: boolean | undefined }, callback: (err: NodeJS.ErrnoException | null, files: Dirent<Buffer>[]) => void): void;
* }} Readdir
*/

/**
* @typedef {{
* (path: PathLike, options?: { encoding: BufferEncoding | null, withFileTypes?: false | undefined, recursive?: boolean | undefined } | BufferEncoding | null): string[];
* (path: PathLike, options?: { encoding: BufferEncoding | null, withFileTypes?: false | undefined, recursive?: boolean | undefined; } | BufferEncoding | null): string[];
* (path: PathLike, options: { encoding: 'buffer', withFileTypes?: false | undefined, recursive?: boolean | undefined } | 'buffer'): Buffer[];
* (path: PathLike, options?: (ObjectEncodingOptions & { withFileTypes?: false | undefined, recursive?: boolean | undefined }) | BufferEncoding | null): string[] | Buffer[];
* (path: PathLike, options: ObjectEncodingOptions & { withFileTypes: true, recursive?: boolean | undefined }): Dirent[];
* (path: PathLike, options: { encoding: "buffer", withFileTypes: true, recursive?: boolean | undefined }): Dirent<Buffer>[];
* }} ReaddirSync
*
* /**
*/

/**
* @typedef {(pathOrFileDescription: PathOrFileDescriptor, callback: ReadJsonCallback) => void} ReadJson
*/

Expand Down
4 changes: 2 additions & 2 deletions lib/SyncAsyncFileSystemDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"use strict";

/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").ReaddirStringCallback} ReaddirStringCallback */
/** @typedef {import("./Resolver").StringCallback} StringCallback */
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */

Expand Down Expand Up @@ -90,14 +89,15 @@ function SyncAsyncFileSystemDecorator(fs) {
result = /** @type {SyncOrAsyncFunction | undefined} */ (callback)
? fs.readdirSync(
arg,
/** @type {Exclude<Parameters<FileSystem["readdir"]>[1], ReaddirStringCallback>} */
/** @type {Exclude<Parameters<FileSystem["readdir"]>[1], (err: NodeJS.ErrnoException | null, files: string[]) => void>} */
(options),
)
: fs.readdirSync(arg);
} catch (err) {
return (callback || options)(
/** @type {NodeJS.ErrnoException | null} */
(err),
[],
);
}

Expand Down
10 changes: 6 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ const fs = require("graceful-fs");
const CachedInputFileSystem = require("./CachedInputFileSystem");
const ResolverFactory = require("./ResolverFactory");

/** @typedef {import("./CachedInputFileSystem").BaseFileSystem} BaseFileSystem */
/** @typedef {import("./PnpPlugin").PnpApiImpl} PnpApi */
/** @typedef {import("./Resolver")} Resolver */
/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
/** @typedef {import("./CachedInputFileSystem").BaseFileSystem} BaseFileSystem */
/** @typedef {import("./Resolver").ResolveCallback} ResolveCallback */
/** @typedef {import("./Resolver").ResolveContext} ResolveContext */
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
/** @typedef {import("./ResolverFactory").Plugin} Plugin */
/** @typedef {import("./ResolverFactory").UserResolveOptions} ResolveOptions */

/**
* @typedef {{
* (context: object, path: string, request: string, resolveContext: ResolveContext, callback: ResolveCallback): void;
Expand All @@ -27,10 +28,11 @@ const ResolverFactory = require("./ResolverFactory");
* (path: string, request: string, callback: ResolveCallback): void;
* }} ResolveFunctionAsync
*/

/**
* @typedef {{
* (context: object, path: string, request: string): string|false;
* (path: string, request: string): string|false;
* (context: object, path: string, request: string): string | false;
* (path: string, request: string): string | false;
* }} ResolveFunction
*/

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
"@eslint/markdown": ">= 6.5.0",
"@types/graceful-fs": "^4.1.6",
"@types/jest": "^27.5.1",
"@types/node": "20.9.5",
"@types/node": "^24.0.3",
"@stylistic/eslint-plugin": ">= 4.4.1",
"cspell": "4.2.8",
"eslint": "^9.28.0",
"eslint-config-prettier": "^10.1.5",
"eslint-config-webpack": "^4.0.2",
"eslint-config-webpack": "^4.1.2",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jest": "^28.13.0",
"eslint-plugin-jsdoc": "^50.7.1",
"eslint-plugin-jest": "^29.0.1",
"eslint-plugin-jsdoc": "^51.2.2",
"eslint-plugin-n": "^17.19.0",
"eslint-plugin-prettier": "^5.4.1",
"eslint-plugin-unicorn": "^59.0.1",
Expand All @@ -41,8 +41,8 @@
"memfs": "^3.2.0",
"prettier": "^3.5.3",
"prettier-2": "npm:prettier@^2",
"tooling": "webpack/tooling#v1.23.10",
"typescript": "^5.3.3"
"tooling": "webpack/tooling#v1.24.0",
"typescript": "^5.8.3"
},
"engines": {
"node": ">=10.13.0"
Expand Down
53 changes: 34 additions & 19 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ declare class CloneBasenamePlugin {
>;
apply(resolver: Resolver): void;
}
declare interface Dirent {
declare interface Dirent<T extends string | Buffer = string> {
/**
* true when is file, otherwise false
*/
Expand Down Expand Up @@ -199,12 +199,17 @@ declare interface Dirent {
/**
* name
*/
name: string;
name: T;

/**
* path
*/
path: string;
parentPath: string;

/**
* path
*/
path?: string;
}
type EncodingOption =
| undefined
Expand Down Expand Up @@ -852,18 +857,14 @@ declare interface Readdir {
withFileTypes?: false;
recursive?: boolean;
},
callback: (err: null | NodeJS.ErrnoException, result?: string[]) => void,
callback: (err: null | NodeJS.ErrnoException, files?: string[]) => void,
): void;
(
path: PathLike,
options:
| { encoding: "buffer"; withFileTypes?: false; recursive?: boolean }
| "buffer",
callback: (err: null | NodeJS.ErrnoException, result?: Buffer[]) => void,
): void;
(
path: PathLike,
callback: (err: null | NodeJS.ErrnoException, result?: string[]) => void,
callback: (err: null | NodeJS.ErrnoException, files?: Buffer[]) => void,
): void;
(
path: PathLike,
Expand All @@ -888,16 +889,31 @@ declare interface Readdir {
}),
callback: (
err: null | NodeJS.ErrnoException,
result?: string[] | Buffer[],
files?: string[] | Buffer[],
) => void,
): void;
(
path: PathLike,
callback: (err: null | NodeJS.ErrnoException, files?: string[]) => void,
): void;
(
path: PathLike,
options: ObjectEncodingOptions & {
withFileTypes: true;
recursive?: boolean;
},
callback: (err: null | NodeJS.ErrnoException, result?: Dirent[]) => void,
callback: (
err: null | NodeJS.ErrnoException,
files?: Dirent<string>[],
) => void,
): void;
(
path: PathLike,
options: { encoding: "buffer"; withFileTypes: true; recursive?: boolean },
callback: (
err: null | NodeJS.ErrnoException,
files: Dirent<Buffer>[],
) => void,
): void;
}
declare interface ReaddirSync {
Expand Down Expand Up @@ -969,7 +985,11 @@ declare interface ReaddirSync {
withFileTypes: true;
recursive?: boolean;
},
): Dirent[];
): Dirent<string>[];
(
path: PathLike,
options: { encoding: "buffer"; withFileTypes: true; recursive?: boolean },
): Dirent<Buffer>[];
}
declare interface Readlink {
(
Expand Down Expand Up @@ -1549,11 +1569,6 @@ declare interface SyncFileSystem {
*/
realpathSync?: RealPathSync;
}

/**
* `URL` class is a global reference for `require('url').URL`
* https://nodejs.org/api/url.html#the-whatwg-url-api
*/
declare interface URL_url extends URL_Import {}
declare interface WriteOnlySet<T> {
add: (item: T) => void;
Expand Down Expand Up @@ -1626,13 +1641,13 @@ declare namespace exports {
CloneBasenamePlugin,
LogInfoPlugin,
ResolveOptionsOptionalFS,
BaseFileSystem,
PnpApi,
Resolver,
FileSystem,
SyncFileSystem,
BaseFileSystem,
ResolveContext,
ResolveRequest,
SyncFileSystem,
Plugin,
ResolveOptionsResolverFactoryObject_2 as ResolveOptions,
ResolveFunctionAsync,
Expand Down
Loading