Recommended way to type useQuery data if we know it cannot be undefined? #1493
-
I have a query that is ran in an Given that, how can I intelligently type Right now I have: // @ts-ignore: data will be AccountResponse here
const { data }: { data: AccountResponse } = useGetAccountQuery(); But I know that this is completely silly and unsafe since, for example, changing the How can I just type This is probably trivial to anyone with more familiarity with TypeScipt than I, so thank you in advance for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hmm. Something like function assertHasData<T>(x: {data?: T}): asserts x is {data: T} {}
const result = useGetAccountQuery();
assertHasData(result)
// result.data is defined here or function assertIsDefined<T>(x: T|undefined): asserts x is T {}
const { data } = useGetAccountQuery();
assertIsDefined(data)
// data is defined here |
Beta Was this translation helpful? Give feedback.
Hmm. Something like
or