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?
Another issue I'm having is if I'm writing a method which, say, returns Result<A, MyError>
and then I do something like
const firstResult = getSomeIntermediateValue(...); // returns Result<B, MyError>
if (firstResult.isErr()) {
return firstResult; // this should typecheck because it's a error of type MyError
}
it doesn't work, and I have to "force" the type eg
const firstResult = getSomeIntermediateValue(...); // returns Result<B, MyError>
if (firstResult.isErr()) {
return err(firstResult.error); // this typechecks as Result<A, MyError> although it's more verbose
}
am I doing something incorrectly?
since some of those methods can't produce an error, they are typed as Result<T, never>
Can I see your myMethod function implementation. is ok(...) only thing return from your function?