neverthrow
neverthrow copied to clipboard
How to best handle Result<T, never>
In order to keep all my API coherent, all my methods return Result types, but since some of those methods can't produce an error, they are typed as Result<T, never>
Then when I consume the result I'm doing eg
const myResult = await myMethod();
if (myResult.isErr()) {
return myResult; // this doesn't type out of the box for some reason, I have to do `return myResult as Err<never, never>` to please the typecheck
}
// otherwise process
const myValue = myResult.value;
another way would be to do
const myResult = await myMethod();
// otherwise process
const myValue = myResult._unsafeUnwrap();
but the documentation says we shouldn't use unsafeUnwrap in prod code ideally
What would be the most natural way to express this:
- I want a coherent API where all methods return a
Resultand therefore some areResult<T, never> - I want the typechecker to somehow understand that it can safely get the value of a method returning a
Result<T, never>because it will never be an error?