neverthrow icon indicating copy to clipboard operation
neverthrow copied to clipboard

Bug (?) with ResultAsync.fromThrowable when function is overloadable.

Open Marley-Mulvin-Broome opened this issue 11 months ago • 2 comments

When using ResultAsync.fromThrowable, there can be some weird typing mishaps that happen with functions that have multiple overloads.

An example of this bug(?) is readFile imported from node:fs/promises. When readFile receives an encoding parameter, the return type will become Promise<string>, but this doesn't seem to happen when used in conjunction with ResultAsync.fromThrowable, as shown below.

// This will be of type Result<string | Buffer<ArrayBufferLike>, unknown>, should be Result<string, unknown>
const result = await ResultAsync.fromThrowable(readFile)('file.txt', 'utf-8');

// When calling the function normally, it will be of type Promise<string> as expected.
const text = await readFile('file.txt', 'utf-8');

To be honest, I don't know if this is an issue with the library itself, or typescript reaching its limits of what it is capable of, but I thought it was worth pointing out as if its fixable that'd be cool.

Sorry if this is a duplicate 🗿

Marley-Mulvin-Broome avatar Jan 24 '25 11:01 Marley-Mulvin-Broome