[ts] how to avoid non-null assertion with dependent queries #2281
Answered
by
TkDodo
stefanprobst
asked this question in
Q&A
-
lots of my dependent query hooks look something like this: const entity = useGetEntityById(
{
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
id: id!,
},
{ enabled: id != null }
); is there a better way which avoids the non-null assertion? |
Beta Was this translation helpful? Give feedback.
Answered by
TkDodo
May 16, 2021
Replies: 1 comment 4 replies
-
As it so happens, I just wrote about this: https://tkdodo.eu/blog/react-query-and-type-script tl;dr: one way is to keep the |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
stefanprobst
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As it so happens, I just wrote about this: https://tkdodo.eu/blog/react-query-and-type-script
tl;dr: one way is to keep the
id
undefined when passing it to thequeryFn
, and handling it there by rejecting the Promise. It's a bit safer because you can bypass theenabled
flag, e.g. with therefetch
function.