Skip to content

Commit

Permalink
docs(angular-query): add TypeScript documentation (#8296)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoud-dv authored Nov 15, 2024
1 parent 8786495 commit 118b152
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@
"label": "Devtools",
"to": "framework/angular/devtools"
},
{
"label": "TypeScript",
"to": "framework/angular/typescript"
},
{
"label": "Zoneless",
"to": "framework/angular/zoneless"
Expand Down
1 change: 1 addition & 0 deletions docs/framework/angular/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { lastValueFrom } from 'rxjs'
import {
injectMutation,
injectQuery,
QueryClient
} from '@tanstack/angular-query-experimental'
@Component({
Expand Down
174 changes: 174 additions & 0 deletions docs/framework/angular/typescript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
id: typescript
title: TypeScript
ref: docs/framework/react/typescript.md
replace:
{
'useQuery': 'injectQuery',
'useMutation': 'injectMutation',
'react-query': 'angular-query-experimental',
'public API of React Query': 'public API of TanStack Query and - after the experimental phase, the angular-query package',
'still follows': 'still follow',
'React Query': 'TanStack Query',
'`success`': '`isSuccess()`',
'function:': 'function.',
'separate function': 'separate function or a service',
}
---

[//]: # 'TypeInference1'

```angular-ts
@Component({
// ...
template: `@let data = query.data();`,
// ^? data: number | undefined
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
}))
}
```

[//]: # 'TypeInference1'
[//]: # 'TypeInference2'

```angular-ts
@Component({
// ...
template: `@let data = query.data();`,
// ^? data: string | undefined
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['test'],
queryFn: () => Promise.resolve(5),
select: (data) => data.toString(),
}))
}
```

[//]: # 'TypeInference2'
[//]: # 'TypeInference3'

In this example we pass Group[] to the type parameter of HttpClient's `get` method.

```angular-ts
@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')),
}))
}
```

[//]: # 'TypeInference3'
[//]: # 'TypeInference4'

```angular-ts
@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'`.
[//]: # 'TypeInference4'
[//]: # 'TypingError'

```angular-ts
@Component({
// ...
template: `@let error = query.error();`,
// ^? error: Error | null
})
class MyComponent {
query = injectQuery(() => ({
queryKey: ['groups'],
queryFn: fetchGroups
}))
}
```

[//]: # 'TypingError'
[//]: # 'TypingError2'

```angular-ts
@Component({
// ...
template: `@let error = query.error();`,
// ^? error: string | null
})
class MyComponent {
query = injectQuery<Group[], string>(() => ({
queryKey: ['groups'],
queryFn: fetchGroups,
}))
}
```

[//]: # 'TypingError2'
[//]: # 'TypingError3'

```ts
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
}
})
```

[//]: # 'TypingError3'
[//]: # 'RegisterErrorType'

```ts
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
})
```

[//]: # 'RegisterErrorType'
[//]: # 'Materials'
[//]: # 'Materials'
2 changes: 1 addition & 1 deletion docs/framework/react/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ declare module '@tanstack/react-query' {

## Typing Query Options

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

```ts
import { queryOptions } from '@tanstack/react-query'
Expand Down

0 comments on commit 118b152

Please sign in to comment.