You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/framework/react/typescript.md
+2-38Lines changed: 2 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,14 +130,14 @@ if (axios.isAxiosError(error)) {
130
130
131
131
### Registering a global Error
132
132
133
-
TanStack Query v5 allows for a way to set a global Error type for everything, without having to specify generics on call-sides, by amending the `Register` interface. This will make sure inference still works, but the error field will be of the specified type. We recommend `unknown` to ensure safe, explicit narrowing at call sites, by amending the `Register` interface:
133
+
TanStack Query v5 allows for a way to set a global Error type for everything, without having to specify generics on call-sides, by amending the `Register` interface. This will make sure inference still works, but the error field will be of the specified type. If you want to enforce that call-sides must do explicit type-narrowing, set `defaultError` to `unknown`:
134
134
135
135
[//]: #'RegisterErrorType'
136
136
137
137
```tsx
138
138
import'@tanstack/react-query'
139
139
140
-
// ✅ Recommended
140
+
141
141
declaremodule'@tanstack/react-query' {
142
142
interfaceRegister {
143
143
// Use unknown so call sites must narrow explicitly.
Queries can throw from multiple places—not only your `queryFn`, but also `select`, memoized callbacks, or other user-land code. If you set a global error type like `AxiosError`, consumers may assume all errors are Axios-driven when many are not. Using `unknown` forces safe, explicit narrowing at usage sites and works for any mixed error source.
157
-
158
-
```tsx
159
-
const { error } =useQuery(options)
160
-
161
-
if (isAxiosError(error)) {
162
-
// handle Axios-specific details
163
-
} elseif (errorinstanceofError) {
164
-
// handle generic errors
165
-
} else {
166
-
// handle non-Error throwables if needed
167
-
}
168
-
```
169
-
170
-
If you want a stable shape across your app, normalize errors where they're thrown and rethrow a domain error:
0 commit comments