TanStack Query is now written in TypeScript to make sure the library and your projects are type-safe!
Things to keep in mind:
Types in TanStack Query generally flow through very well so that you don't have to provide type annotations for yourself
@Component({
// ...
template: `@let data = query.data();`,
// ^? data: number | undefined
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
}))
}
@Component({
// ...
template: `@let data = query.data();`,
// ^? data: number | undefined
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
}))
}
@Component({
// ...
template: `@let data = query.data();`,
// ^? data: string | undefined
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
select: (data) => data.toString(),
}))
}
@Component({
// ...
template: `@let data = query.data();`,
// ^? data: string | undefined
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
select: (data) => data.toString(),
}))
}
This works best if your queryFn has a well-defined returned type. Keep in mind that most data fetching libraries return any per default, so make sure to extract it to a properly typed function.
In this example we pass Group[] to the type parameter of HttpClient's get method.
@Component({
template: `@let data = query.data();`,
// ^? data: Group[] | undefined
})
class MyComponent {
http = inject(HttpClient)
query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: () => lastValueFrom(this.http.get<Group[]>('/groups')),
}))
}
@Component({
template: `@let data = query.data();`,
// ^? data: Group[] | undefined
})
class MyComponent {
http = inject(HttpClient)
query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: () => lastValueFrom(this.http.get<Group[]>('/groups')),
}))
}
TanStack Query uses a discriminated union type for the query result, discriminated by the status field and the derived status boolean flags. This will allow you to check for e.g. isSuccess() status to make data defined:
@Component({
// ...
template: `
@if (query.isSuccess()) {
@let data = query.data();
// ^? data: number
}
`,
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
}))
}
@Component({
// ...
template: `
@if (query.isSuccess()) {
@let data = query.data();
// ^? data: number
}
`,
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
}))
}
TypeScript currently does not support discriminated unions on object methods. Narrowing on signal fields on objects such as query results only works on signals returning a boolean. Prefer using isSuccess() and similar boolean status signals over status() === 'success'.
The type for error defaults to Error, because that is what most users expect.
@Component({
// ...
template: `@let error = query.error();`,
// ^? error: Error | null
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: fetchGroups
}))
}
@Component({
// ...
template: `@let error = query.error();`,
// ^? error: Error | null
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: fetchGroups
}))
}
If you want to throw a custom error, or something that isn't an Error at all, you can specify the type of the error field:
@Component({
// ...
template: `@let error = query.error();`,
// ^? error: string | null
})
class MyComponent {
query = injectQuery<Group[], string>(() => ({
queryKey: ['groups'],
queryFn: fetchGroups,
}))
}
@Component({
// ...
template: `@let error = query.error();`,
// ^? error: string | null
})
class MyComponent {
query = injectQuery<Group[], string>(() => ({
queryKey: ['groups'],
queryFn: fetchGroups,
}))
}
However, this has the drawback that type inference for all other generics of injectQuery will not work anymore. It is generally not considered a good practice to throw something that isn't an Error, so if you have a subclass like AxiosError you can use type narrowing to make the error field more specific:
import axios from 'axios'
query = injectQuery(() => ({ queryKey: ['groups'], queryFn: fetchGroups }))
computed(() => {
const error = query.error()
// ^? error: Error | null
if (axios.isAxiosError(error)) {
error
// ^? const error: AxiosError
}
})
import axios from 'axios'
query = injectQuery(() => ({ queryKey: ['groups'], queryFn: fetchGroups }))
computed(() => {
const error = query.error()
// ^? error: Error | null
if (axios.isAxiosError(error)) {
error
// ^? const error: AxiosError
}
})
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:
import '@tanstack/angular-query-experimental'
declare module '@tanstack/angular-query-experimental' {
interface Register {
defaultError: AxiosError
}
}
const query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: fetchGroups,
}))
computed(() => {
const error = query.error()
// ^? error: AxiosError | null
})
import '@tanstack/angular-query-experimental'
declare module '@tanstack/angular-query-experimental' {
interface Register {
defaultError: AxiosError
}
}
const query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: fetchGroups,
}))
computed(() => {
const error = query.error()
// ^? error: AxiosError | null
})
Similarly to registering a global error type you can also register a global Meta type. This ensures the optional meta field on queries and mutations stays consistent and is type-safe. Note that the registered type must extend Record<string, unknown> so that meta remains an object.
import '@tanstack/angular-query-experimental'
interface MyMeta extends Record<string, unknown> {
// Your meta type definition.
}
declare module '@tanstack/angular-query-experimental' {
interface Register {
queryMeta: MyMeta
mutationMeta: MyMeta
}
}
import '@tanstack/angular-query-experimental'
interface MyMeta extends Record<string, unknown> {
// Your meta type definition.
}
declare module '@tanstack/angular-query-experimental' {
interface Register {
queryMeta: MyMeta
mutationMeta: MyMeta
}
}
If you inline query options into injectQuery, you'll get automatic type inference. However, you might want to extract the query options into a separate function to share them between injectQuery and e.g. prefetchQuery or manage them in a service. In that case, you'd lose type inference. To get it back, you can use the queryOptions helper:
@Injectable({
providedIn: 'root',
})
export class QueriesService {
private http = inject(HttpClient)
post(postId: number) {
return queryOptions({
queryKey: ['post', postId],
queryFn: () => {
return lastValueFrom(
this.http.get<Post>(
`https://jsonplaceholder.typicode.com/posts/${postId}`,
),
)
},
})
}
}
@Component({
// ...
})
export class Component {
queryClient = inject(QueryClient)
postId = signal(1)
queries = inject(QueriesService)
optionsSignal = computed(() => this.queries.post(this.postId()))
postQuery = injectQuery(() => this.queries.post(1))
postQuery = injectQuery(() => this.queries.post(this.postId()))
// You can also pass a signal which returns query options
postQuery = injectQuery(this.optionsSignal)
someMethod() {
this.queryClient.prefetchQuery(this.queries.post(23))
}
}
@Injectable({
providedIn: 'root',
})
export class QueriesService {
private http = inject(HttpClient)
post(postId: number) {
return queryOptions({
queryKey: ['post', postId],
queryFn: () => {
return lastValueFrom(
this.http.get<Post>(
`https://jsonplaceholder.typicode.com/posts/${postId}`,
),
)
},
})
}
}
@Component({
// ...
})
export class Component {
queryClient = inject(QueryClient)
postId = signal(1)
queries = inject(QueriesService)
optionsSignal = computed(() => this.queries.post(this.postId()))
postQuery = injectQuery(() => this.queries.post(1))
postQuery = injectQuery(() => this.queries.post(this.postId()))
// You can also pass a signal which returns query options
postQuery = injectQuery(this.optionsSignal)
someMethod() {
this.queryClient.prefetchQuery(this.queries.post(23))
}
}
Further, the queryKey returned from queryOptions knows about the queryFn associated with it, and we can leverage that type information to make functions like queryClient.getQueryData aware of those types as well:
data = this.queryClient.getQueryData(groupOptions().queryKey)
// ^? data: Post | undefined
data = this.queryClient.getQueryData(groupOptions().queryKey)
// ^? data: Post | undefined
Without queryOptions, the type of data would be unknown, unless we'd pass a type parameter:
data = queryClient.getQueryData<Post>(['post', 1])
data = queryClient.getQueryData<Post>(['post', 1])
Similarly to queryOptions, you can use mutationOptions to extract mutation options into a separate function:
export class QueriesService {
private http = inject(HttpClient)
updatePost(id: number) {
return mutationOptions({
mutationFn: (post: Post) => Promise.resolve(post),
mutationKey: ['updatePost', id],
onSuccess: (newPost) => {
// ^? newPost: Post
this.queryClient.setQueryData(['posts', id], newPost)
},
})
}
}
export class QueriesService {
private http = inject(HttpClient)
updatePost(id: number) {
return mutationOptions({
mutationFn: (post: Post) => Promise.resolve(post),
mutationKey: ['updatePost', id],
onSuccess: (newPost) => {
// ^? newPost: Post
this.queryClient.setQueryData(['posts', id], newPost)
},
})
}
}
If you are using TypeScript, you can use the skipToken to disable a query. This is useful when you want to disable a query based on a condition, but you still want to keep the query to be type safe. Read more about it in the Disabling Queries guide.