Skip to content

Commit 0738d17

Browse files
committed
Update code to match thunk rework
1 parent a9362fb commit 0738d17

14 files changed

+93
-63
lines changed

Diff for: packages/toolkit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
"dependencies": {
119119
"immer": "^10.0.3",
120120
"redux": "^5.0.1",
121-
"redux-thunk": "^3.1.0",
121+
"redux-thunk": "https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz",
122122
"reselect": "^5.1.0"
123123
},
124124
"peerDependencies": {

Diff for: packages/toolkit/src/createAsyncThunk.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
ActionCreatorWithPreparedPayload,
55
} from './createAction'
66
import { createAction } from './createAction'
7-
import type { ThunkDispatch } from 'redux-thunk'
7+
import type { ThunkAction, ThunkDispatch } from 'redux-thunk'
88
import type {
99
ActionFromMatcher,
1010
FallbackIfUnknown,
@@ -239,19 +239,20 @@ export type AsyncThunkAction<
239239
Returned,
240240
ThunkArg,
241241
ThunkApiConfig extends AsyncThunkConfig,
242-
> = (
243-
dispatch: NonNullable<GetDispatch<ThunkApiConfig>>,
244-
getState: () => GetState<ThunkApiConfig>,
245-
extra: GetExtra<ThunkApiConfig>,
246-
) => SafePromise<
247-
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>>
248-
| ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>
249-
> & {
250-
abort: (reason?: string) => void
251-
requestId: string
252-
arg: ThunkArg
253-
unwrap: () => Promise<Returned>
254-
}
242+
> = ThunkAction<
243+
NonNullable<GetDispatch<ThunkApiConfig>>,
244+
GetState<ThunkApiConfig>,
245+
GetExtra<ThunkApiConfig>,
246+
SafePromise<
247+
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>>
248+
| ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>
249+
> & {
250+
abort: (reason?: string) => void
251+
requestId: string
252+
arg: ThunkArg
253+
unwrap: () => Promise<Returned>
254+
}
255+
>
255256

256257
type AsyncThunkActionCreator<
257258
Returned,

Diff for: packages/toolkit/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ export {
2222
createDraftSafeSelector,
2323
createDraftSafeSelectorCreator,
2424
} from './createDraftSafeSelector'
25-
export type { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk'
25+
export type {
26+
ThunkAction,
27+
ThunkOverload,
28+
ThunkDispatch,
29+
ThunkMiddleware,
30+
} from 'redux-thunk'
2631

2732
export {
2833
// js

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ type AppStore = typeof store
5959
type AppDispatch = typeof store.dispatch
6060
type RootState = ReturnType<typeof store.getState>
6161
type AppThunk<ThunkReturnType = void> = ThunkAction<
62-
ThunkReturnType,
62+
AppDispatch,
6363
RootState,
6464
unknown,
65-
Action
65+
ThunkReturnType
6666
>
6767

6868
describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {

Diff for: packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ type AppStore = typeof store
4949
type AppDispatch = typeof store.dispatch
5050
type RootState = ReturnType<typeof store.getState>
5151
type AppThunk<ThunkReturnType = void> = ThunkAction<
52-
ThunkReturnType,
52+
AppDispatch,
5353
RootState,
5454
unknown,
55-
Action
55+
ThunkReturnType
5656
>
5757

5858
const listenerMiddleware = createListenerMiddleware()

Diff for: packages/toolkit/src/query/core/buildInitiate.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
UnknownAction,
1212
ThunkAction,
1313
SerializedError,
14+
ThunkDispatch,
1415
} from '@reduxjs/toolkit'
1516
import type { SubscriptionOptions, RootState } from './apiState'
1617
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
@@ -58,7 +59,12 @@ type StartQueryActionCreator<
5859
> = (
5960
arg: QueryArgFrom<D>,
6061
options?: StartQueryActionCreatorOptions,
61-
) => ThunkAction<QueryActionCreatorResult<D>, any, any, UnknownAction>
62+
) => ThunkAction<
63+
ThunkDispatch<any, any, UnknownAction>,
64+
any,
65+
any,
66+
QueryActionCreatorResult<D>
67+
>
6268

6369
export type QueryActionCreatorResult<
6470
D extends QueryDefinition<any, any, any, any>,
@@ -88,7 +94,12 @@ type StartMutationActionCreator<
8894
track?: boolean
8995
fixedCacheKey?: string
9096
},
91-
) => ThunkAction<MutationActionCreatorResult<D>, any, any, UnknownAction>
97+
) => ThunkAction<
98+
ThunkDispatch<any, any, UnknownAction>,
99+
any,
100+
any,
101+
MutationActionCreatorResult<D>
102+
>
92103

93104
export type MutationActionCreatorResult<
94105
D extends MutationDefinition<any, any, any, any>,

Diff for: packages/toolkit/src/query/core/buildThunks.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ export type PatchQueryDataThunk<
168168
args: QueryArgFrom<Definitions[EndpointName]>,
169169
patches: readonly Patch[],
170170
updateProvided?: boolean,
171-
) => ThunkAction<void, PartialState, any, UnknownAction>
171+
) => ThunkAction<
172+
ThunkDispatch<PartialState, any, UnknownAction>,
173+
PartialState,
174+
any,
175+
void
176+
>
172177

173178
export type UpdateQueryDataThunk<
174179
Definitions extends EndpointDefinitions,
@@ -178,7 +183,12 @@ export type UpdateQueryDataThunk<
178183
args: QueryArgFrom<Definitions[EndpointName]>,
179184
updateRecipe: Recipe<ResultTypeFrom<Definitions[EndpointName]>>,
180185
updateProvided?: boolean,
181-
) => ThunkAction<PatchCollection, PartialState, any, UnknownAction>
186+
) => ThunkAction<
187+
ThunkDispatch<PartialState, any, UnknownAction>,
188+
PartialState,
189+
any,
190+
PatchCollection
191+
>
182192

183193
export type UpsertQueryDataThunk<
184194
Definitions extends EndpointDefinitions,
@@ -188,14 +198,14 @@ export type UpsertQueryDataThunk<
188198
args: QueryArgFrom<Definitions[EndpointName]>,
189199
value: ResultTypeFrom<Definitions[EndpointName]>,
190200
) => ThunkAction<
201+
ThunkDispatch<PartialState, any, UnknownAction>,
202+
PartialState,
203+
any,
191204
QueryActionCreatorResult<
192205
Definitions[EndpointName] extends QueryDefinition<any, any, any, any>
193206
? Definitions[EndpointName]
194207
: never
195-
>,
196-
PartialState,
197-
any,
198-
UnknownAction
208+
>
199209
>
200210

201211
/**
@@ -608,7 +618,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
608618
endpointName: EndpointName,
609619
arg: any,
610620
options: PrefetchOptions,
611-
): ThunkAction<void, any, any, UnknownAction> =>
621+
): ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, void> =>
612622
(dispatch: ThunkDispatch<any, any, any>, getState: () => any) => {
613623
const force = hasTheForce(options) && options.force
614624
const maxAge = hasMaxAge(options) && options.ifOlderThan

Diff for: packages/toolkit/src/query/core/module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export type CoreModule =
7373
| ReferenceCacheCollection
7474

7575
export interface ThunkWithReturnValue<T>
76-
extends ThunkAction<T, any, any, UnknownAction> {}
76+
extends ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, T> {}
7777

7878
declare module '../apiTypes' {
7979
export interface ApiModules<
@@ -222,7 +222,7 @@ declare module '../apiTypes' {
222222
endpointName: EndpointName,
223223
arg: QueryArgFrom<Definitions[EndpointName]>,
224224
options: PrefetchOptions,
225-
): ThunkAction<void, any, any, UnknownAction>
225+
): ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, void>
226226
/**
227227
* A Redux thunk action creator that, when dispatched, creates and applies a set of JSON diff/patch objects to the current state. This immediately updates the Redux state with those changes.
228228
*

Diff for: packages/toolkit/src/query/react/buildHooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ type GenericPrefetchThunk = (
637637
endpointName: any,
638638
arg: any,
639639
options: PrefetchOptions,
640-
) => ThunkAction<void, any, any, UnknownAction>
640+
) => ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, void>
641641

642642
/**
643643
*

Diff for: packages/toolkit/src/tests/configureStore.test-d.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
ThunkAction,
1111
ThunkDispatch,
1212
ThunkMiddleware,
13+
ThunkOverload,
1314
UnknownAction,
1415
} from '@reduxjs/toolkit'
1516
import {
@@ -150,9 +151,7 @@ describe('type tests', () => {
150151
enhancers: () => [enhancer],
151152
})
152153

153-
expectTypeOf(store.dispatch).toMatchTypeOf<
154-
Dispatch & ThunkDispatch<number, undefined, UnknownAction>
155-
>()
154+
expectTypeOf(store.dispatch).toMatchTypeOf<Dispatch>()
156155

157156
configureStore({
158157
reducer: () => 0,
@@ -202,9 +201,7 @@ describe('type tests', () => {
202201
.concat(somePropertyStoreEnhancer),
203202
})
204203

205-
expectTypeOf(store3.dispatch).toMatchTypeOf<
206-
Dispatch & ThunkDispatch<number, undefined, UnknownAction>
207-
>()
204+
expectTypeOf(store3.dispatch).toMatchTypeOf<Dispatch>()
208205

209206
expectTypeOf(store3.someProperty).toBeString()
210207

@@ -423,7 +420,12 @@ describe('type tests', () => {
423420
type StateA = number
424421
const reducerA = () => 0
425422
const thunkA = () => {
426-
return (() => {}) as any as ThunkAction<Promise<'A'>, StateA, any, any>
423+
return (() => {}) as any as ThunkAction<
424+
ThunkDispatch<StateA, any, any>,
425+
StateA,
426+
any,
427+
Promise<'A'>
428+
>
427429
}
428430

429431
type StateB = string
@@ -560,35 +562,35 @@ describe('type tests', () => {
560562
const store = configureStore({ reducer: {} })
561563
// undefined is the default value for the ThunkMiddleware extraArgument
562564
store.dispatch(function () {} as ThunkAction<
563-
void,
565+
ThunkDispatch<{}, undefined, UnknownAction>,
564566
{},
565567
undefined,
566-
UnknownAction
568+
void
567569
>)
568570
// `null` for the `extra` generic was previously documented in the RTK "Advanced Tutorial", but
569571
// is a bad pattern and users should use `unknown` instead
570572
// @ts-expect-error
571573
store.dispatch(function () {} as ThunkAction<
572-
void,
574+
ThunkDispatch<{}, null, UnknownAction>,
573575
{},
574576
null,
575-
UnknownAction
577+
void
576578
>)
577579
// unknown is the best way to type a ThunkAction if you do not care
578580
// about the value of the extraArgument, as it will always work with every
579581
// ThunkMiddleware, no matter the actual extraArgument type
580582
store.dispatch(function () {} as ThunkAction<
581-
void,
583+
ThunkDispatch<{}, unknown, UnknownAction>,
582584
{},
583585
unknown,
584-
UnknownAction
586+
void
585587
>)
586588
// @ts-expect-error
587589
store.dispatch(function () {} as ThunkAction<
588-
void,
590+
ThunkDispatch<{}, boolean, UnknownAction>,
589591
{},
590592
boolean,
591-
UnknownAction
593+
void
592594
>)
593595
})
594596

@@ -792,7 +794,7 @@ describe('type tests', () => {
792794
// the thunk middleware type kicks in and TS thinks a plain action is being returned
793795
expectTypeOf(store.dispatch).toEqualTypeOf<
794796
((action: Action<'actionListenerMiddleware/add'>) => Unsubscribe) &
795-
ThunkDispatch<CounterState, undefined, UnknownAction> &
797+
ThunkOverload<CounterState, undefined> &
796798
Dispatch<UnknownAction>
797799
>()
798800

Diff for: packages/toolkit/src/tests/getDefaultMiddleware.test-d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ describe('type tests', () => {
148148
(storeApi) => (next) => (action) => {}
149149

150150
const testThunk: ThunkAction<
151-
void,
151+
ThunkDispatch<{ counter: number }, number, UnknownAction>,
152152
{ counter: number },
153153
number,
154-
UnknownAction
154+
void
155155
> = (dispatch, getState, extraArg) => {
156156
expect(extraArg).toBe(extraArgument)
157157
}

Diff for: packages/toolkit/src/tests/getDefaultMiddleware.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
Action,
44
Middleware,
55
ThunkAction,
6+
ThunkDispatch,
67
UnknownAction,
78
} from '@reduxjs/toolkit'
89
import { configureStore } from '@reduxjs/toolkit'
@@ -88,10 +89,10 @@ describe('getDefaultMiddleware', () => {
8889
(storeApi) => (next) => (action) => {}
8990

9091
const testThunk: ThunkAction<
91-
void,
92+
ThunkDispatch<{ counter: number }, number, UnknownAction>,
9293
{ counter: number },
9394
number,
94-
UnknownAction
95+
void
9596
> = (dispatch, getState, extraArg) => {
9697
expect(extraArg).toBe(extraArgument)
9798
}

Diff for: packages/toolkit/src/tests/matchers.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
createReducer,
1414
} from '@reduxjs/toolkit'
1515

16-
const thunk: ThunkAction<any, any, any, UnknownAction> = () => {}
16+
const thunk: ThunkAction<any, any, any, any> = () => {}
1717

1818
describe('isAnyOf', () => {
1919
it('returns true only if any matchers match (match function)', () => {

Diff for: yarn.lock

+10-10
Original file line numberDiff line numberDiff line change
@@ -7481,7 +7481,7 @@ __metadata:
74817481
prettier: "npm:^3.2.5"
74827482
query-string: "npm:^7.0.1"
74837483
redux: "npm:^5.0.1"
7484-
redux-thunk: "npm:^3.1.0"
7484+
redux-thunk: "https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz"
74857485
reselect: "npm:^5.1.0"
74867486
rimraf: "npm:^3.0.2"
74877487
size-limit: "npm:^11.0.1"
@@ -24680,6 +24680,15 @@ __metadata:
2468024680
languageName: node
2468124681
linkType: hard
2468224682

24683+
"redux-thunk@https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz":
24684+
version: 3.1.0
24685+
resolution: "redux-thunk@https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz"
24686+
peerDependencies:
24687+
redux: ^5.0.0
24688+
checksum: 10/c47c69d216747dbb6294239a76ca4b9a3ce82b0bb0272213f7bec7e8b4abbfb334a1a2dfe2211fa2202742f5969795e065ba0f6cf6dd128e351851d5fababb2c
24689+
languageName: node
24690+
linkType: hard
24691+
2468324692
"redux-thunk@npm:^2.4.1":
2468424693
version: 2.4.1
2468524694
resolution: "redux-thunk@npm:2.4.1"
@@ -24698,15 +24707,6 @@ __metadata:
2469824707
languageName: node
2469924708
linkType: hard
2470024709

24701-
"redux-thunk@npm:^3.1.0":
24702-
version: 3.1.0
24703-
resolution: "redux-thunk@npm:3.1.0"
24704-
peerDependencies:
24705-
redux: ^5.0.0
24706-
checksum: 10/38c563db5f0bbec90d2e65cc27f3c870c1b6102e0c071258734fac41cb0e51d31d894125815c2f4133b20aff231f51f028ad99bccc05a7e3249f1a5d5a959ed3
24707-
languageName: node
24708-
linkType: hard
24709-
2471024710
"redux@npm:^4.1.2":
2471124711
version: 4.1.2
2471224712
resolution: "redux@npm:4.1.2"

0 commit comments

Comments
 (0)