Skip to content

How to best handle Result<T, never> #655

@maelp

Description

@maelp

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 Result and therefore some are Result<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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions