Skip to content

How to properly update the error type of a query/mutation result? #5784

Answered by TkDodo
Ionshard asked this question in Q&A
Discussion options

You must be logged in to vote

The issue is at the bottom of the reproduction in the useGetTask hook.

it looks like re-assigning result.error doesn't change the type of error. I think you would need a type assertion here:

  if(result.isError){
    result.error = parseError(result.error);
    return result as UseQueryResult<Task, ErrorResult>
  }

however, it still doesn't check out because you return result at the very end, and there, you haven't transformed the error. Is there a reason why you can't always call parseError and handle the null check inside of it? That would enable you to at least do:

  const result = useQuery({
    queryKey: ['getTask', id],
    queryFn: () => getTask(id)
  })

  result.error = parseE…

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@Ionshard
Comment options

@TkDodo
Comment options

@Ionshard
Comment options

@TkDodo
Comment options

@Ionshard
Comment options

Answer selected by Ionshard
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