|
| 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' |
0 commit comments