diff --git a/docs/framework/angular/typescript.md b/docs/framework/angular/typescript.md index 6a10e940bdb..0aa2ff26388 100644 --- a/docs/framework/angular/typescript.md +++ b/docs/framework/angular/typescript.md @@ -153,7 +153,8 @@ import '@tanstack/angular-query-experimental' declare module '@tanstack/angular-query-experimental' { interface Register { - defaultError: AxiosError + // Use unknown so call sites must narrow explicitly. + defaultError: unknown } } @@ -164,7 +165,7 @@ const query = injectQuery(() => ({ computed(() => { const error = query.error() - // ^? error: AxiosError | null + // ^? error: unknown | null }) ``` diff --git a/docs/framework/react/typescript.md b/docs/framework/react/typescript.md index 98a5c3e8ea2..cddd6e6b97b 100644 --- a/docs/framework/react/typescript.md +++ b/docs/framework/react/typescript.md @@ -130,7 +130,7 @@ if (axios.isAxiosError(error)) { ### Registering a global Error -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: +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`: [//]: # 'RegisterErrorType' @@ -139,12 +139,13 @@ import '@tanstack/react-query' declare module '@tanstack/react-query' { interface Register { - defaultError: AxiosError + // Use unknown so call sites must narrow explicitly. + defaultError: unknown } } const { error } = useQuery({ queryKey: ['groups'], queryFn: fetchGroups }) -// ^? const error: AxiosError | null +// ^? const error: unknown | null ``` [//]: # 'RegisterErrorType' diff --git a/docs/framework/solid/typescript.md b/docs/framework/solid/typescript.md index aa6bd28a8fc..67711cbabcf 100644 --- a/docs/framework/solid/typescript.md +++ b/docs/framework/solid/typescript.md @@ -129,16 +129,15 @@ if (axios.isAxiosError(query.error)) { [typescript playground](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYygUwIYzQRQK5pQCecAvnAGZQQhwDkAAjBgHYDOzyA1gPRsQAbYABMAtAEcCxOgFgAUKEiw4GAB7AIbStVp01GtrLnyYRMGjgBxanjBwAvIjgiAXHBZ4QAI0Jl585Ah2eAo0GGQAC2sIWy1HAAoASjcABR1gNjQAHmjbAG0AXQA+BxL9TQA6AHMw+LoeKpswQ0SKmAi0Fnj0Nkh2C3sSnr7MiuEsDET-OUDguElCEkdUTGx8Rfik0rh4hHk4A-mpIgBpNCI3PLpGmOa6AoAaOH3DheIAMRY3UPCoprYHvJSIkpsY5G8iBVCNQoPIeDxDnAAHoAfmmwAoO3KbAqGQAgupNABRKAw+IQqGk6AgxAvA4U6HQOlweGI1FA+RAA) -## Registering a global `Error` - -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: +[//]: # 'RegisterErrorType' ```tsx import '@tanstack/solid-query' declare module '@tanstack/solid-query' { interface Register { - defaultError: AxiosError + // Use unknown so call sites must narrow explicitly. + defaultError: unknown } } @@ -148,9 +147,11 @@ const query = useQuery(() => ({ })) query.error -// ^? (property) error: AxiosError | null +// ^? (property) error: unknown | null ``` +[//]: # 'RegisterErrorType' + ## Registering global `Meta` 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` so that `meta` remains an object. diff --git a/docs/framework/vue/typescript.md b/docs/framework/vue/typescript.md index 731c276a244..7cf70dbc4f3 100644 --- a/docs/framework/vue/typescript.md +++ b/docs/framework/vue/typescript.md @@ -89,6 +89,21 @@ if (error.value instanceof Error) { [//]: # 'TypingError3' [//]: # 'TypingError3' [//]: # 'RegisterErrorType' + +```tsx +import '@tanstack/vue-query' + +declare module '@tanstack/vue-query' { + interface Register { + // Use unknown so call sites must narrow explicitly. + defaultError: unknown + } +} + +const { error } = useQuery({ queryKey: ['groups'], queryFn: fetchGroups }) +// ^? const error: unknown | null +``` + [//]: # 'RegisterErrorType' [//]: # 'TypingMeta' [//]: # 'TypingMeta'