Skip to content

Commit cbdd25e

Browse files
authored
Merge pull request #2120 from FaberVitale/docs/alm-update-ts-usage
2 parents 0f76067 + 1e841c2 commit cbdd25e

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

docs/api/createListenerMiddleware.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,13 @@ To fix this, the middleware provides types for defining "pre-typed" versions of
391391
import { createListenerMiddleware, addListener } from '@reduxjs/toolkit'
392392
import type { TypedStartListening, TypedAddListener } from '@reduxjs/toolkit'
393393

394-
import type { RootState } from './store'
394+
import type { RootState, AppDispatch } from './store'
395395

396396
export const listenerMiddleware = createListenerMiddleware()
397397

398398
export const startAppListening =
399-
listenerMiddleware.startListening as TypedStartListening<RootState>
400-
export const addAppListener = addListener as TypedAddListener<RootState>
399+
listenerMiddleware.startListening as TypedStartListening<RootState, AppDispatch>
400+
export const addAppListener = addListener as TypedAddListener<RootState, AppDispatch>
401401
```
402402

403403
Then import and use those pre-typed methods in your components.

examples/action-listener/counter/src/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ export { store }
2626

2727
// Infer the `RootState` and `AppDispatch` types from the store itself
2828
export type RootState = ReturnType<typeof store.getState>
29-
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
29+
// @see https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type
3030
export type AppDispatch = typeof store.dispatch
3131

3232
export type AppListenerEffectAPI = ListenerEffectAPI<RootState, AppDispatch>
3333

34+
// @see https://redux-toolkit.js.org/api/createListenerMiddleware#typescript-usage
3435
export type AppStartListening = TypedStartListening<RootState, AppDispatch>
35-
export type AppAddListener = TypedAddListener<RootState>
36+
export type AppAddListener = TypedAddListener<RootState, AppDispatch>
3637

3738
export const startAppListening =
3839
listenerMiddlewareInstance.startListening as AppStartListening

0 commit comments

Comments
 (0)