memfs icon indicating copy to clipboard operation
memfs copied to clipboard

Wrong type for readFile in .promises

Open reverofevil opened this issue 3 years ago • 1 comments

Compare

readFile(id: TFileHandle, options?: IReadFileOptions | string): Promise<TDataOut>;

to Node's

function readFile(path: PathLike | FileHandle, options?: { encoding?: null, flag?: OpenMode } & Abortable | null): Promise<Buffer>;
function readFile(path: PathLike | FileHandle, options: { encoding: BufferEncoding, flag?: OpenMode } & Abortable | BufferEncoding): Promise<string>;
function readFile(path: PathLike | FileHandle, options?: BaseEncodingOptions & Abortable & { flag?: OpenMode } | BufferEncoding | null): Promise<string | Buffer>;

For a case when BufferEncoding is present, result type has to be string. Why don't just copy (or reuse, if memfs is a drop-in replacement) typings from @types/node?

UPD.

declare const promises: Omit<
    (typeof import("fs"))["promises"],
    "read" | "write" | "ftruncate" | "rm" | "fdatasync" | "fsync" | "fchmod" | "lutimes" | "fchown" | "futimes" | "opendir" | "watch"
>;

should do the trick

reverofevil avatar Jun 17 '21 12:06 reverofevil

I'm running into this issue too image

Typecasting with as string for now to work around

Tobbe avatar Mar 27 '24 10:03 Tobbe