Skip to content

fix(fs-node): infer promises.readFile results from encoding - #1299

Open
Godsdar wants to merge 1 commit into
streamich:masterfrom
Godsdar:fix/promises-readfile-encoding
Open

Godsdar wants to merge 1 commit into
streamich:masterfrom
Godsdar:fix/promises-readfile-encoding

Conversation

@Godsdar

@Godsdar Godsdar commented Oct 17, 2026

Copy link
Copy Markdown

fs.promises.readFile('/foo', 'utf8') currently returns Promise<string | Buffer> in TypeScript, forcing callers to cast before using string methods.

Add encoding-sensitive overloads to both exported FsPromisesApi interfaces and share that signature with the concrete class. Text encodings infer Promise<string>; omitted encoding and memfs's 'buffer' encoding infer Promise<Buffer>. Dynamic strings/options retain the union. Emitted JavaScript is unchanged.

Add regression tests for encoding strings/objects, flags and binary results. Before the fix, these fail with six TS2322 assignment errors.

Fixes #702.

Validated locally on macOS arm64, Node 26.9.0 / Yarn 4.12.0:

  • yarn build, yarn typecheck, yarn prettier:check: passed.
  • yarn test: 100 suites passed; 1,619 tests passed, 18 skipped; 14 snapshots passed.
  • git diff --check: passed.
  • Targeted TSLint: three pre-existing failures in FsPromises.ts — duplicate imports at line 7 and two Function type bans at line 21. Baseline and patched lint results are identical; no new lint failures.

Scope is promise API readFile inference; FileHandle.readFile and other method overloads are separate work. No dependency changes. The full upstream OS/Node CI matrix has not been run locally.

AI assistance was used for investigation, implementation, testing and agent code review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong type for readFile in .promises

1 participant