react-query@tsnext feedback #814
Replies: 2 comments 8 replies
-
|
Hi @wolverineks, thanks for giving feedback on the new types! I'll try to answer your questions below. Because typings and defaults are also a matter of taste, everything is open for debate :) Discriminated unions // A common pattern is to destructure the query result, but in doing so, the unions would have no effect:
const { error, data } = useQuery()
// In case someone would use the status property to discriminate:
const result = useQuery()
if (result.status === 'idle') {
// We already know there is no `data` or `error`
}
if (result.status === 'loading') {
// Still need to assert `data` or `error`
}
if (result.status === 'success') {
// Now `data` is available, but we could have also asserted `data`
}
if (result.status === 'error') {
// Now `error` is available, but we could have also asserted `error`
}Default error type in queries and mutations // In this case the error would be a `string`:
const { error } = useQuery('post', () => {
return Promise.reject('rejected')
})
// In this case the error would be an `AxiosError`:
const { error } = useQuery(['post', 1], (key, id) => {
return axios.get(`/post/${id.toString()}`)
})
// In this case the error would be an `Error`:
const { error } = useQuery(['post', undefined], (key, id) => {
return axios.get(`/post/${id.toString()}`)
})I think it is good practise to actually check the error type and mimics the direction TypeScript is going: https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#unknown-on-catch . You can however still explicitly define the error type or create your own Suspense |
Beta Was this translation helpful? Give feedback.
-
|
Is there any way to make the status be |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
useQueryunknown? can it default toError?suspense: true, typeof data should not includeundefineduseMutationTResultandTVariablesget inferred from mutationFn, andTErrordefaults toErrorBeta Was this translation helpful? Give feedback.
All reactions