Skip to content

Infer properly Error of a Result #82

Closed Answered by zoontek
Nainterceptor asked this question in Q&A
Discussion options

You must be logged in to vote

@Nainterceptor That's normal, because in your example, you have this:

type InferError<T> = T extends Result<any, infer E> ? E : never;

And this:

function Parent(): Result<
  null,
  InferError<typeof Child1> | InferError<typeof Child2>
>

But typeof Child1 and typeof Child2 are not of type Result, but of type () => Result<null, Child1Error> and () => Result<null, Child2Error>.

To fix it, you can ever either use ReturnType:

function Parent(): Result<
  null,
  InferError<ReturnType<typeof Child1> | ReturnType<typeof Child2>>
>

Or change your InferError (and set a constraint on the generic to have nice reporting in case you pass something else):

type InferError<T extends (...args: any[]) => R…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Nainterceptor
Comment options

@zoontek
Comment options

Answer selected by Nainterceptor
@Nainterceptor
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants