supermacro / neverthrow

Type-Safe Errors for JS & TypeScript
MIT License
4.06k stars 85 forks source link

prevent passing a specific error type for `.fromThrowable` without providing an error mapping function #583

Open boyeln opened 2 months ago

boyeln commented 2 months ago

As discussed in https://github.com/supermacro/neverthrow/pull/567#discussion_r1754313850, the current type signature of fromThrowable allows users to pass a specific error value type, without providing an error mapping function that produces the provided error type:

const safeParse = Result.fromThrowable<typeof JSON.parse, number>(JSON.parse);
      // ^ (text: string, reviver?: (this: any, key: string, value: any) => any) => Result<any, number>

This PR tries to solve that by adding some overloads to the function, only allowing users to specify the error type when a matching error mapping function is provided.

changeset-bot[bot] commented 2 months ago

🦋 Changeset detected

Latest commit: 67360794043d21244fafe1dea0c088d5fdd0cd2f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------- | ----- | | neverthrow | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR