Skip to content

Commit 2989e69

Browse files
leesb971204TkDodoautofix-ci[bot]
authored
docs(react-query): recommend defaultError = unknown instead of AxiosError (#9575)
* docs(react-query): recommend defaultError = unknown instead of AxiosError Signed-off-by: leesb971204 <[email protected]> * docs(react-query): clarify usage of defaultError in global Error type registration Signed-off-by: leesb971204 <[email protected]> * ci: apply automated fixes * docs(solid-query): recommend defaultError = unknown instead of AxiosError Signed-off-by: leesb971204 <[email protected]> * docs(angular-query): recommend defaultError = unknown instead of AxiosError Signed-off-by: leesb971204 <[email protected]> * docs(vue-query): add example code for Registering a global Error Signed-off-by: leesb971204 <[email protected]> --------- Signed-off-by: leesb971204 <[email protected]> Co-authored-by: Dominik Dorfmeister <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent f1e608b commit 2989e69

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

docs/framework/angular/typescript.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ import '@tanstack/angular-query-experimental'
153153

154154
declare module '@tanstack/angular-query-experimental' {
155155
interface Register {
156-
defaultError: AxiosError
156+
// Use unknown so call sites must narrow explicitly.
157+
defaultError: unknown
157158
}
158159
}
159160

@@ -164,7 +165,7 @@ const query = injectQuery(() => ({
164165

165166
computed(() => {
166167
const error = query.error()
167-
// ^? error: AxiosError | null
168+
// ^? error: unknown | null
168169
})
169170
```
170171

docs/framework/react/typescript.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ if (axios.isAxiosError(error)) {
130130

131131
### Registering a global Error
132132

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:
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`:
134134

135135
[//]: # 'RegisterErrorType'
136136

@@ -139,12 +139,13 @@ import '@tanstack/react-query'
139139

140140
declare module '@tanstack/react-query' {
141141
interface Register {
142-
defaultError: AxiosError
142+
// Use unknown so call sites must narrow explicitly.
143+
defaultError: unknown
143144
}
144145
}
145146

146147
const { error } = useQuery({ queryKey: ['groups'], queryFn: fetchGroups })
147-
// ^? const error: AxiosError | null
148+
// ^? const error: unknown | null
148149
```
149150

150151
[//]: # 'RegisterErrorType'

docs/framework/solid/typescript.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,15 @@ if (axios.isAxiosError(query.error)) {
129129

130130
[typescript playground](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYygUwIYzQRQK5pQCecAvnAGZQQhwDkAAjBgHYDOzyA1gPRsQAbYABMAtAEcCxOgFgAUKEiw4GAB7AIbStVp01GtrLnyYRMGjgBxanjBwAvIjgiAXHBZ4QAI0Jl585Ah2eAo0GGQAC2sIWy1HAAoASjcABR1gNjQAHmjbAG0AXQA+BxL9TQA6AHMw+LoeKpswQ0SKmAi0Fnj0Nkh2C3sSnr7MiuEsDET-OUDguElCEkdUTGx8Rfik0rh4hHk4A-mpIgBpNCI3PLpGmOa6AoAaOH3DheIAMRY3UPCoprYHvJSIkpsY5G8iBVCNQoPIeDxDnAAHoAfmmwAoO3KbAqGQAgupNABRKAw+IQqGk6AgxAvA4U6HQOlweGI1FA+RAA)
131131

132-
## Registering a global `Error`
133-
134-
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:
132+
[//]: # 'RegisterErrorType'
135133

136134
```tsx
137135
import '@tanstack/solid-query'
138136

139137
declare module '@tanstack/solid-query' {
140138
interface Register {
141-
defaultError: AxiosError
139+
// Use unknown so call sites must narrow explicitly.
140+
defaultError: unknown
142141
}
143142
}
144143

@@ -148,9 +147,11 @@ const query = useQuery(() => ({
148147
}))
149148

150149
query.error
151-
// ^? (property) error: AxiosError | null
150+
// ^? (property) error: unknown | null
152151
```
153152

153+
[//]: # 'RegisterErrorType'
154+
154155
## Registering global `Meta`
155156

156157
Similarly to registering a [global error type](#registering-a-global-error) you can also register a global `Meta` type. This ensures the optional `meta` field on [queries](../reference/useQuery.md) and [mutations](../reference/useMutation.md) stays consistent and is type-safe. Note that the registered type must extend `Record<string, unknown>` so that `meta` remains an object.

docs/framework/vue/typescript.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ if (error.value instanceof Error) {
8989
[//]: # 'TypingError3'
9090
[//]: # 'TypingError3'
9191
[//]: # 'RegisterErrorType'
92+
93+
```tsx
94+
import '@tanstack/vue-query'
95+
96+
declare module '@tanstack/vue-query' {
97+
interface Register {
98+
// Use unknown so call sites must narrow explicitly.
99+
defaultError: unknown
100+
}
101+
}
102+
103+
const { error } = useQuery({ queryKey: ['groups'], queryFn: fetchGroups })
104+
// ^? const error: unknown | null
105+
```
106+
92107
[//]: # 'RegisterErrorType'
93108
[//]: # 'TypingMeta'
94109
[//]: # 'TypingMeta'

0 commit comments

Comments
 (0)