Skip to content

Commit 68e438c

Browse files
committed
docs: add tutorials/typescript
1 parent 21876ce commit 68e438c

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

docs/tutorials/typescript.md

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,29 @@ hide_title: true
77

88
 
99

10-
# React Redux TypeScript Quick Start
10+
# Angular Redux TypeScript Quick Start
1111

1212
:::tip What You'll Learn
1313

14-
- How to set up and use Redux Toolkit and React Redux with TypeScript
14+
- How to set up and use Redux Toolkit and Angular Redux with TypeScript
1515

1616
:::
1717

1818
:::info Prerequisites
1919

20-
- Knowledge of React [Hooks](https://react.dev/reference/react#)
20+
- Knowledge of Angular [Signals](https://angular.dev/guide/signals)
2121
- Understanding of [Redux terms and concepts](https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow)
2222
- Understanding of TypeScript syntax and concepts
2323

2424
:::
2525

2626
## Introduction
2727

28-
Welcome to the React Redux TypeScript Quick Start tutorial! **This tutorial will briefly show how to use TypeScript with Redux Toolkit and React-Redux**.
28+
Welcome to the Angular Redux TypeScript Quick Start tutorial! **This tutorial will briefly show how to use TypeScript with Redux Toolkit and Angular-Redux**.
2929

30-
This page focuses on just how to set up the TypeScript aspects . For explanations of what Redux is, how it works, and full examples of how to use Redux, see [the Redux core docs tutorials](https://redux.js.org/tutorials/index).
30+
This page focuses on just how to set up the TypeScript aspects. For explanations of what Redux is, how it works, and full examples of how to use Redux, see [the Redux core docs tutorials](https://redux.js.org/tutorials/index).
3131

32-
[React Redux](https://react-redux.js.org) is also written in TypeScript as of version 8, and also includes its own type definitions.
33-
34-
The [Redux+TS template for Create-React-App](https://github.com/reduxjs/cra-template-redux-typescript) comes with a working example of these patterns already configured.
35-
36-
:::info
37-
38-
The recently updated `@types/react@18` major version has changed component definitions to remove having `children` as a prop by default. This causes errors if you have multiple copies of `@types/react` in your project. To fix this, tell your package manager to resolve `@types/react` to a single version. Details:
39-
40-
https://github.com/facebook/react/issues/24304#issuecomment-1094565891
41-
42-
:::
32+
[Angular Redux](/) is also written in TypeScript, and also includes its own type definitions.
4333

4434
## Project Setup
4535

@@ -69,23 +59,23 @@ export type AppDispatch = typeof store.dispatch
6959
// highlight-end
7060
```
7161
72-
### Define Typed Hooks
62+
### Define Typed Injectables
7363
74-
While it's possible to import the `RootState` and `AppDispatch` types into each component, it's **better to create typed versions of the `useDispatch` and `useSelector` hooks for usage in your application**. This is important for a couple reasons:
64+
While it's possible to import the `RootState` and `AppDispatch` types into each component, it's **better to create typed versions of the `injectDispatch` and `injectSelector` injectables for usage in your application**. This is important for a couple reasons:
7565
76-
- For `useSelector`, it saves you the need to type `(state: RootState)` every time
77-
- For `useDispatch`, the default `Dispatch` type does not know about thunks. In order to correctly dispatch thunks, you need to use the specific customized `AppDispatch` type from the store that includes the thunk middleware types, and use that with `useDispatch`. Adding a pre-typed `useDispatch` hook keeps you from forgetting to import `AppDispatch` where it's needed.
66+
- For `injectSelector`, it saves you the need to type `(state: RootState)` every time
67+
- For `injectDispatch`, the default `Dispatch` type does not know about thunks. In order to correctly dispatch thunks, you need to use the specific customized `AppDispatch` type from the store that includes the thunk middleware types, and use that with `injectDispatch`. Adding a pre-typed `injectDispatch` injectable keeps you from forgetting to import `AppDispatch` where it's needed.
7868
79-
Since these are actual variables, not types, it's important to define them in a separate file such as `app/hooks.ts`, not the store setup file. This allows you to import them into any component file that needs to use the hooks, and avoids potential circular import dependency issues.
69+
Since these are actual variables, not types, it's important to define them in a separate file such as `app/injectables.ts`, not the store setup file. This allows you to import them into any component file that needs to use the injectables, and avoids potential circular import dependency issues.
8070
81-
```ts title="app/hooks.ts"
82-
import { useDispatch, useSelector } from 'react-redux'
71+
```ts title="app/injectables.ts"
72+
import { injectDispatch, injectSelector } from '@reduxjs/angular-redux'
8373
import type { RootState, AppDispatch } from './store'
8474

8575
// highlight-start
86-
// Use throughout your app instead of plain `useDispatch` and `useSelector`
87-
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
88-
export const useAppSelector = useSelector.withTypes<RootState>()
76+
// Use throughout your app instead of plain `injectDispatch` and `injectSelector`
77+
export const injectAppDispatch = injectDispatch.withTypes<AppDispatch>()
78+
export const injectAppSelector = injectSelector.withTypes<RootState>()
8979
// highlight-end
9080
```
9181

@@ -154,29 +144,32 @@ const initialState = {
154144
} as CounterState
155145
```
156146

157-
### Use Typed Hooks in Components
147+
### Use Typed Injectables in Components
158148

159-
In component files, import the pre-typed hooks instead of the standard hooks from React-Redux.
160-
161-
```tsx title="features/counter/Counter.tsx"
162-
import React, { useState } from 'react'
149+
In component files, import the pre-typed injectables instead of the standard injectables from Angular-Redux.
163150

151+
```typescript title="features/counter/counter.component.ts"
152+
import { Component } from '@angular/core'
164153
// highlight-next-line
165-
import { useAppSelector, useAppDispatch } from 'app/hooks'
166-
167-
import { decrement, increment } from './counterSlice'
154+
import { injectAppSelector, injectAppDispatch } from "app/injectables";
155+
import { decrement, increment } from './store/counter-slice'
168156

169-
export function Counter() {
157+
@Component({
158+
selector: 'app-counter',
159+
standalone: true,
160+
// omit rendering logic
161+
})
162+
export class CounterComponent {
170163
// highlight-start
171164
// The `state` arg is correctly typed as `RootState` already
172-
const count = useAppSelector((state) => state.counter.value)
173-
const dispatch = useAppDispatch()
165+
count = injectAppSelector(state => state.counter.value)
166+
dispatch = injectAppDispatch()
174167
// highlight-end
175-
176-
// omit rendering logic
168+
increment = increment
169+
decrement = decrement
177170
}
178171
```
179172

180173
## What's Next?
181174

182-
See [the "Usage with TypeScript" page](../using-react-redux/usage-with-typescript.md) for extended details on how to use Redux Toolkit's APIs with TypeScript.
175+
See [the "Usage with TypeScript" page](../using-angular-redux/usage-with-typescript.md) for extended details on how to use Redux Toolkit's APIs with TypeScript.

0 commit comments

Comments
 (0)