Skip to content

Commit 15c0f92

Browse files
committed
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit into improve-treeshakeability
2 parents a3874b8 + ae1dcbf commit 15c0f92

File tree

4 files changed

+116
-42
lines changed

4 files changed

+116
-42
lines changed

docs/api/createListenerMiddleware.mdx

+4-1
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,14 @@ To fix this, the middleware provides types for defining "pre-typed" versions of
488488
import { createListenerMiddleware, addListener } from '@reduxjs/toolkit'
489489
import type { RootState, AppDispatch } from './store'
490490

491+
declare type ExtraArgument = { foo: string }
492+
491493
export const listenerMiddleware = createListenerMiddleware()
492494

493495
export const startAppListening = listenerMiddleware.startListening.withTypes<
494496
RootState,
495-
AppDispatch
497+
AppDispatch,
498+
ExtraArgument
496499
>()
497500

498501
export const addAppListener = addListener.withTypes<RootState, AppDispatch>()

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test-d.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type AppThunk<ThunkReturnType = void> = ThunkAction<
6464
unknown,
6565
Action
6666
>
67+
type ExtraArgument = { foo: string }
6768

6869
describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
6970
const listenerMiddleware = createListenerMiddleware()
@@ -77,11 +78,12 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
7778
test('startListening.withTypes', () => {
7879
const startAppListening = listenerMiddleware.startListening.withTypes<
7980
RootState,
80-
AppDispatch
81+
AppDispatch,
82+
ExtraArgument
8183
>()
8284

8385
expectTypeOf(startAppListening).toEqualTypeOf<
84-
TypedStartListening<RootState, AppDispatch>
86+
TypedStartListening<RootState, AppDispatch, ExtraArgument>
8587
>()
8688

8789
startAppListening({
@@ -102,6 +104,8 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
102104

103105
expectTypeOf(stateCurrent).toEqualTypeOf<RootState>()
104106

107+
expectTypeOf(listenerApi.extra).toEqualTypeOf<ExtraArgument>()
108+
105109
timeout = 1
106110
takeResult = await listenerApi.take(increment.match, timeout)
107111

@@ -111,10 +115,14 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
111115
})
112116

113117
test('addListener.withTypes', () => {
114-
const addAppListener = addListener.withTypes<RootState, AppDispatch>()
118+
const addAppListener = addListener.withTypes<
119+
RootState,
120+
AppDispatch,
121+
ExtraArgument
122+
>()
115123

116124
expectTypeOf(addAppListener).toEqualTypeOf<
117-
TypedAddListener<RootState, AppDispatch>
125+
TypedAddListener<RootState, AppDispatch, ExtraArgument>
118126
>()
119127

120128
store.dispatch(
@@ -126,27 +134,34 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
126134
expectTypeOf(state).toEqualTypeOf<RootState>()
127135

128136
expectTypeOf(listenerApi.dispatch).toEqualTypeOf<AppDispatch>()
137+
138+
expectTypeOf(listenerApi.extra).toEqualTypeOf<ExtraArgument>()
129139
},
130140
}),
131141
)
132142
})
133143

134144
test('removeListener.withTypes', () => {
135-
const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
145+
const removeAppListener = removeListener.withTypes<
146+
RootState,
147+
AppDispatch,
148+
ExtraArgument
149+
>()
136150

137151
expectTypeOf(removeAppListener).toEqualTypeOf<
138-
TypedRemoveListener<RootState, AppDispatch>
152+
TypedRemoveListener<RootState, AppDispatch, ExtraArgument>
139153
>()
140154
})
141155

142156
test('stopListening.withTypes', () => {
143157
const stopAppListening = listenerMiddleware.stopListening.withTypes<
144158
RootState,
145-
AppDispatch
159+
AppDispatch,
160+
ExtraArgument
146161
>()
147162

148163
expectTypeOf(stopAppListening).toEqualTypeOf<
149-
TypedStopListening<RootState, AppDispatch>
164+
TypedStopListening<RootState, AppDispatch, ExtraArgument>
150165
>()
151166
})
152167
})

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,33 @@ type AppThunk<ThunkReturnType = void> = ThunkAction<
5555
Action
5656
>
5757

58+
type ExtraArgument = { foo: string }
59+
5860
const listenerMiddleware = createListenerMiddleware()
5961

6062
const startAppListening = listenerMiddleware.startListening.withTypes<
6163
RootState,
62-
AppDispatch
64+
AppDispatch,
65+
ExtraArgument
6366
>()
6467

6568
const stopAppListening = listenerMiddleware.stopListening.withTypes<
6669
RootState,
67-
AppDispatch
70+
AppDispatch,
71+
ExtraArgument
6872
>()
6973

70-
const addAppListener = addListener.withTypes<RootState, AppDispatch>()
74+
const addAppListener = addListener.withTypes<
75+
RootState,
76+
AppDispatch,
77+
ExtraArgument
78+
>()
7179

72-
const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
80+
const removeAppListener = removeListener.withTypes<
81+
RootState,
82+
AppDispatch,
83+
ExtraArgument
84+
>()
7385

7486
describe('startAppListening.withTypes', () => {
7587
test('should return startListening', () => {

0 commit comments

Comments
 (0)