Skip to content

Commit 118b152

Browse files
authoredNov 15, 2024··
docs(angular-query): add TypeScript documentation (#8296)
1 parent 8786495 commit 118b152

File tree

4 files changed

+180
-1
lines changed

4 files changed

+180
-1
lines changed
 

‎docs/config.json

+4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@
145145
"label": "Devtools",
146146
"to": "framework/angular/devtools"
147147
},
148+
{
149+
"label": "TypeScript",
150+
"to": "framework/angular/typescript"
151+
},
148152
{
149153
"label": "Zoneless",
150154
"to": "framework/angular/zoneless"

‎docs/framework/angular/quick-start.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { lastValueFrom } from 'rxjs'
5151
import {
5252
injectMutation,
5353
injectQuery,
54+
QueryClient
5455
} from '@tanstack/angular-query-experimental'
5556
5657
@Component({

‎docs/framework/angular/typescript.md

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
id: typescript
3+
title: TypeScript
4+
ref: docs/framework/react/typescript.md
5+
replace:
6+
{
7+
'useQuery': 'injectQuery',
8+
'useMutation': 'injectMutation',
9+
'react-query': 'angular-query-experimental',
10+
'public API of React Query': 'public API of TanStack Query and - after the experimental phase, the angular-query package',
11+
'still follows': 'still follow',
12+
'React Query': 'TanStack Query',
13+
'`success`': '`isSuccess()`',
14+
'function:': 'function.',
15+
'separate function': 'separate function or a service',
16+
}
17+
---
18+
19+
[//]: # 'TypeInference1'
20+
21+
```angular-ts
22+
@Component({
23+
// ...
24+
template: `@let data = query.data();`,
25+
// ^? data: number | undefined
26+
})
27+
class MyComponent {
28+
query = injectQuery(() => ({
29+
queryKey: ['test'],
30+
queryFn: () => Promise.resolve(5),
31+
}))
32+
}
33+
```
34+
35+
[//]: # 'TypeInference1'
36+
[//]: # 'TypeInference2'
37+
38+
```angular-ts
39+
@Component({
40+
// ...
41+
template: `@let data = query.data();`,
42+
// ^? data: string | undefined
43+
})
44+
class MyComponent {
45+
query = injectQuery(() => ({
46+
queryKey: ['test'],
47+
queryFn: () => Promise.resolve(5),
48+
select: (data) => data.toString(),
49+
}))
50+
}
51+
```
52+
53+
[//]: # 'TypeInference2'
54+
[//]: # 'TypeInference3'
55+
56+
In this example we pass Group[] to the type parameter of HttpClient's `get` method.
57+
58+
```angular-ts
59+
@Component({
60+
template: `@let data = query.data();`,
61+
// ^? data: Group[] | undefined
62+
})
63+
class MyComponent {
64+
http = inject(HttpClient)
65+
66+
query = injectQuery(() => ({
67+
queryKey: ['groups'],
68+
queryFn: () => lastValueFrom(this.http.get<Group[]>('/groups')),
69+
}))
70+
}
71+
```
72+
73+
[//]: # 'TypeInference3'
74+
[//]: # 'TypeInference4'
75+
76+
```angular-ts
77+
@Component({
78+
// ...
79+
template: `
80+
@if (query.isSuccess()) {
81+
@let data = query.data();
82+
// ^? data: number
83+
}
84+
`,
85+
})
86+
class MyComponent {
87+
query = injectQuery(() => ({
88+
queryKey: ['test'],
89+
queryFn: () => Promise.resolve(5),
90+
}))
91+
}
92+
```
93+
94+
> 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'`.
95+
96+
[//]: # 'TypeInference4'
97+
[//]: # 'TypingError'
98+
99+
```angular-ts
100+
@Component({
101+
// ...
102+
template: `@let error = query.error();`,
103+
// ^? error: Error | null
104+
})
105+
class MyComponent {
106+
query = injectQuery(() => ({
107+
queryKey: ['groups'],
108+
queryFn: fetchGroups
109+
}))
110+
}
111+
```
112+
113+
[//]: # 'TypingError'
114+
[//]: # 'TypingError2'
115+
116+
```angular-ts
117+
@Component({
118+
// ...
119+
template: `@let error = query.error();`,
120+
// ^? error: string | null
121+
})
122+
class MyComponent {
123+
query = injectQuery<Group[], string>(() => ({
124+
queryKey: ['groups'],
125+
queryFn: fetchGroups,
126+
}))
127+
}
128+
```
129+
130+
[//]: # 'TypingError2'
131+
[//]: # 'TypingError3'
132+
133+
```ts
134+
import axios from 'axios'
135+
136+
query = injectQuery(() => ({ queryKey: ['groups'], queryFn: fetchGroups }))
137+
138+
computed(() => {
139+
const error = query.error()
140+
// ^? error: Error | null
141+
142+
if (axios.isAxiosError(error)) {
143+
error
144+
// ^? const error: AxiosError
145+
}
146+
})
147+
```
148+
149+
[//]: # 'TypingError3'
150+
[//]: # 'RegisterErrorType'
151+
152+
```ts
153+
import '@tanstack/angular-query-experimental'
154+
155+
declare module '@tanstack/angular-query-experimental' {
156+
interface Register {
157+
defaultError: AxiosError
158+
}
159+
}
160+
161+
const query = injectQuery(() => ({
162+
queryKey: ['groups'],
163+
queryFn: fetchGroups,
164+
}))
165+
166+
computed(() => {
167+
const error = query.error()
168+
// ^? error: AxiosError | null
169+
})
170+
```
171+
172+
[//]: # 'RegisterErrorType'
173+
[//]: # 'Materials'
174+
[//]: # 'Materials'

‎docs/framework/react/typescript.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ declare module '@tanstack/react-query' {
176176

177177
## Typing Query Options
178178

179-
If you inline query options into `useQuery`, you'll get automatic type inference. However, you might want to extract the query options into a separate function to share them between `useQuery` and e.g. `prefetchQuery`. In that case, you'd lose type inference. To get it back, you can use `queryOptions` helper:
179+
If you inline query options into `useQuery`, you'll get automatic type inference. However, you might want to extract the query options into a separate function to share them between `useQuery` and e.g. `prefetchQuery`. In that case, you'd lose type inference. To get it back, you can use the `queryOptions` helper:
180180

181181
```ts
182182
import { queryOptions } from '@tanstack/react-query'

0 commit comments

Comments
 (0)
Please sign in to comment.