From 0738d17b2e772f9d6547b9542d55b9937884ea3a Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 30 Jun 2024 00:33:12 +0100 Subject: [PATCH] Update code to match thunk rework --- packages/toolkit/package.json | 2 +- packages/toolkit/src/createAsyncThunk.ts | 29 ++++++++-------- packages/toolkit/src/index.ts | 7 +++- .../listenerMiddleware.withTypes.test-d.ts | 4 +-- .../listenerMiddleware.withTypes.test.ts | 4 +-- .../toolkit/src/query/core/buildInitiate.ts | 15 ++++++-- .../toolkit/src/query/core/buildThunks.ts | 24 +++++++++---- packages/toolkit/src/query/core/module.ts | 4 +-- .../toolkit/src/query/react/buildHooks.ts | 2 +- .../src/tests/configureStore.test-d.ts | 34 ++++++++++--------- .../src/tests/getDefaultMiddleware.test-d.ts | 4 +-- .../src/tests/getDefaultMiddleware.test.ts | 5 +-- packages/toolkit/src/tests/matchers.test.ts | 2 +- yarn.lock | 20 +++++------ 14 files changed, 93 insertions(+), 63 deletions(-) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index a76f7a9ccb..4a03f8c440 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -118,7 +118,7 @@ "dependencies": { "immer": "^10.0.3", "redux": "^5.0.1", - "redux-thunk": "^3.1.0", + "redux-thunk": "https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz", "reselect": "^5.1.0" }, "peerDependencies": { diff --git a/packages/toolkit/src/createAsyncThunk.ts b/packages/toolkit/src/createAsyncThunk.ts index e7ee39a05a..a7274c2856 100644 --- a/packages/toolkit/src/createAsyncThunk.ts +++ b/packages/toolkit/src/createAsyncThunk.ts @@ -4,7 +4,7 @@ import type { ActionCreatorWithPreparedPayload, } from './createAction' import { createAction } from './createAction' -import type { ThunkDispatch } from 'redux-thunk' +import type { ThunkAction, ThunkDispatch } from 'redux-thunk' import type { ActionFromMatcher, FallbackIfUnknown, @@ -239,19 +239,20 @@ export type AsyncThunkAction< Returned, ThunkArg, ThunkApiConfig extends AsyncThunkConfig, -> = ( - dispatch: NonNullable>, - getState: () => GetState, - extra: GetExtra, -) => SafePromise< - | ReturnType> - | ReturnType> -> & { - abort: (reason?: string) => void - requestId: string - arg: ThunkArg - unwrap: () => Promise -} +> = ThunkAction< + NonNullable>, + GetState, + GetExtra, + SafePromise< + | ReturnType> + | ReturnType> + > & { + abort: (reason?: string) => void + requestId: string + arg: ThunkArg + unwrap: () => Promise + } +> type AsyncThunkActionCreator< Returned, diff --git a/packages/toolkit/src/index.ts b/packages/toolkit/src/index.ts index 61893b8349..6e2035601a 100644 --- a/packages/toolkit/src/index.ts +++ b/packages/toolkit/src/index.ts @@ -22,7 +22,12 @@ export { createDraftSafeSelector, createDraftSafeSelectorCreator, } from './createDraftSafeSelector' -export type { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk' +export type { + ThunkAction, + ThunkOverload, + ThunkDispatch, + ThunkMiddleware, +} from 'redux-thunk' export { // js diff --git a/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test-d.ts b/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test-d.ts index 6756144579..1763b0d83a 100644 --- a/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test-d.ts +++ b/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test-d.ts @@ -59,10 +59,10 @@ type AppStore = typeof store type AppDispatch = typeof store.dispatch type RootState = ReturnType type AppThunk = ThunkAction< - ThunkReturnType, + AppDispatch, RootState, unknown, - Action + ThunkReturnType > describe('listenerMiddleware.withTypes()', () => { diff --git a/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts b/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts index 84bcf1ff2c..c5c838fb46 100644 --- a/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts +++ b/packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts @@ -49,10 +49,10 @@ type AppStore = typeof store type AppDispatch = typeof store.dispatch type RootState = ReturnType type AppThunk = ThunkAction< - ThunkReturnType, + AppDispatch, RootState, unknown, - Action + ThunkReturnType > const listenerMiddleware = createListenerMiddleware() diff --git a/packages/toolkit/src/query/core/buildInitiate.ts b/packages/toolkit/src/query/core/buildInitiate.ts index 3823b14d79..08e783920e 100644 --- a/packages/toolkit/src/query/core/buildInitiate.ts +++ b/packages/toolkit/src/query/core/buildInitiate.ts @@ -11,6 +11,7 @@ import type { UnknownAction, ThunkAction, SerializedError, + ThunkDispatch, } from '@reduxjs/toolkit' import type { SubscriptionOptions, RootState } from './apiState' import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs' @@ -58,7 +59,12 @@ type StartQueryActionCreator< > = ( arg: QueryArgFrom, options?: StartQueryActionCreatorOptions, -) => ThunkAction, any, any, UnknownAction> +) => ThunkAction< + ThunkDispatch, + any, + any, + QueryActionCreatorResult +> export type QueryActionCreatorResult< D extends QueryDefinition, @@ -88,7 +94,12 @@ type StartMutationActionCreator< track?: boolean fixedCacheKey?: string }, -) => ThunkAction, any, any, UnknownAction> +) => ThunkAction< + ThunkDispatch, + any, + any, + MutationActionCreatorResult +> export type MutationActionCreatorResult< D extends MutationDefinition, diff --git a/packages/toolkit/src/query/core/buildThunks.ts b/packages/toolkit/src/query/core/buildThunks.ts index b461ace39d..c017d4902d 100644 --- a/packages/toolkit/src/query/core/buildThunks.ts +++ b/packages/toolkit/src/query/core/buildThunks.ts @@ -168,7 +168,12 @@ export type PatchQueryDataThunk< args: QueryArgFrom, patches: readonly Patch[], updateProvided?: boolean, -) => ThunkAction +) => ThunkAction< + ThunkDispatch, + PartialState, + any, + void +> export type UpdateQueryDataThunk< Definitions extends EndpointDefinitions, @@ -178,7 +183,12 @@ export type UpdateQueryDataThunk< args: QueryArgFrom, updateRecipe: Recipe>, updateProvided?: boolean, -) => ThunkAction +) => ThunkAction< + ThunkDispatch, + PartialState, + any, + PatchCollection +> export type UpsertQueryDataThunk< Definitions extends EndpointDefinitions, @@ -188,14 +198,14 @@ export type UpsertQueryDataThunk< args: QueryArgFrom, value: ResultTypeFrom, ) => ThunkAction< + ThunkDispatch, + PartialState, + any, QueryActionCreatorResult< Definitions[EndpointName] extends QueryDefinition ? Definitions[EndpointName] : never - >, - PartialState, - any, - UnknownAction + > > /** @@ -608,7 +618,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".` endpointName: EndpointName, arg: any, options: PrefetchOptions, - ): ThunkAction => + ): ThunkAction, any, any, void> => (dispatch: ThunkDispatch, getState: () => any) => { const force = hasTheForce(options) && options.force const maxAge = hasMaxAge(options) && options.ifOlderThan diff --git a/packages/toolkit/src/query/core/module.ts b/packages/toolkit/src/query/core/module.ts index c237c7b298..fe6c0ee5eb 100644 --- a/packages/toolkit/src/query/core/module.ts +++ b/packages/toolkit/src/query/core/module.ts @@ -73,7 +73,7 @@ export type CoreModule = | ReferenceCacheCollection export interface ThunkWithReturnValue - extends ThunkAction {} + extends ThunkAction, any, any, T> {} declare module '../apiTypes' { export interface ApiModules< @@ -222,7 +222,7 @@ declare module '../apiTypes' { endpointName: EndpointName, arg: QueryArgFrom, options: PrefetchOptions, - ): ThunkAction + ): ThunkAction, any, any, void> /** * 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. * diff --git a/packages/toolkit/src/query/react/buildHooks.ts b/packages/toolkit/src/query/react/buildHooks.ts index 06b7596385..629f5b06f1 100644 --- a/packages/toolkit/src/query/react/buildHooks.ts +++ b/packages/toolkit/src/query/react/buildHooks.ts @@ -637,7 +637,7 @@ type GenericPrefetchThunk = ( endpointName: any, arg: any, options: PrefetchOptions, -) => ThunkAction +) => ThunkAction, any, any, void> /** * diff --git a/packages/toolkit/src/tests/configureStore.test-d.ts b/packages/toolkit/src/tests/configureStore.test-d.ts index 2bf27f4bdc..fda6415192 100644 --- a/packages/toolkit/src/tests/configureStore.test-d.ts +++ b/packages/toolkit/src/tests/configureStore.test-d.ts @@ -10,6 +10,7 @@ import type { ThunkAction, ThunkDispatch, ThunkMiddleware, + ThunkOverload, UnknownAction, } from '@reduxjs/toolkit' import { @@ -150,9 +151,7 @@ describe('type tests', () => { enhancers: () => [enhancer], }) - expectTypeOf(store.dispatch).toMatchTypeOf< - Dispatch & ThunkDispatch - >() + expectTypeOf(store.dispatch).toMatchTypeOf() configureStore({ reducer: () => 0, @@ -202,9 +201,7 @@ describe('type tests', () => { .concat(somePropertyStoreEnhancer), }) - expectTypeOf(store3.dispatch).toMatchTypeOf< - Dispatch & ThunkDispatch - >() + expectTypeOf(store3.dispatch).toMatchTypeOf() expectTypeOf(store3.someProperty).toBeString() @@ -423,7 +420,12 @@ describe('type tests', () => { type StateA = number const reducerA = () => 0 const thunkA = () => { - return (() => {}) as any as ThunkAction, StateA, any, any> + return (() => {}) as any as ThunkAction< + ThunkDispatch, + StateA, + any, + Promise<'A'> + > } type StateB = string @@ -560,35 +562,35 @@ describe('type tests', () => { const store = configureStore({ reducer: {} }) // undefined is the default value for the ThunkMiddleware extraArgument store.dispatch(function () {} as ThunkAction< - void, + ThunkDispatch<{}, undefined, UnknownAction>, {}, undefined, - UnknownAction + void >) // `null` for the `extra` generic was previously documented in the RTK "Advanced Tutorial", but // is a bad pattern and users should use `unknown` instead // @ts-expect-error store.dispatch(function () {} as ThunkAction< - void, + ThunkDispatch<{}, null, UnknownAction>, {}, null, - UnknownAction + void >) // unknown is the best way to type a ThunkAction if you do not care // about the value of the extraArgument, as it will always work with every // ThunkMiddleware, no matter the actual extraArgument type store.dispatch(function () {} as ThunkAction< - void, + ThunkDispatch<{}, unknown, UnknownAction>, {}, unknown, - UnknownAction + void >) // @ts-expect-error store.dispatch(function () {} as ThunkAction< - void, + ThunkDispatch<{}, boolean, UnknownAction>, {}, boolean, - UnknownAction + void >) }) @@ -792,7 +794,7 @@ describe('type tests', () => { // the thunk middleware type kicks in and TS thinks a plain action is being returned expectTypeOf(store.dispatch).toEqualTypeOf< ((action: Action<'actionListenerMiddleware/add'>) => Unsubscribe) & - ThunkDispatch & + ThunkOverload & Dispatch >() diff --git a/packages/toolkit/src/tests/getDefaultMiddleware.test-d.ts b/packages/toolkit/src/tests/getDefaultMiddleware.test-d.ts index 50680a09ce..74d422d231 100644 --- a/packages/toolkit/src/tests/getDefaultMiddleware.test-d.ts +++ b/packages/toolkit/src/tests/getDefaultMiddleware.test-d.ts @@ -148,10 +148,10 @@ describe('type tests', () => { (storeApi) => (next) => (action) => {} const testThunk: ThunkAction< - void, + ThunkDispatch<{ counter: number }, number, UnknownAction>, { counter: number }, number, - UnknownAction + void > = (dispatch, getState, extraArg) => { expect(extraArg).toBe(extraArgument) } diff --git a/packages/toolkit/src/tests/getDefaultMiddleware.test.ts b/packages/toolkit/src/tests/getDefaultMiddleware.test.ts index b72a959b0b..773c801c8c 100644 --- a/packages/toolkit/src/tests/getDefaultMiddleware.test.ts +++ b/packages/toolkit/src/tests/getDefaultMiddleware.test.ts @@ -3,6 +3,7 @@ import type { Action, Middleware, ThunkAction, + ThunkDispatch, UnknownAction, } from '@reduxjs/toolkit' import { configureStore } from '@reduxjs/toolkit' @@ -88,10 +89,10 @@ describe('getDefaultMiddleware', () => { (storeApi) => (next) => (action) => {} const testThunk: ThunkAction< - void, + ThunkDispatch<{ counter: number }, number, UnknownAction>, { counter: number }, number, - UnknownAction + void > = (dispatch, getState, extraArg) => { expect(extraArg).toBe(extraArgument) } diff --git a/packages/toolkit/src/tests/matchers.test.ts b/packages/toolkit/src/tests/matchers.test.ts index 11a6fdb8cd..0cf119ff53 100644 --- a/packages/toolkit/src/tests/matchers.test.ts +++ b/packages/toolkit/src/tests/matchers.test.ts @@ -13,7 +13,7 @@ import { createReducer, } from '@reduxjs/toolkit' -const thunk: ThunkAction = () => {} +const thunk: ThunkAction = () => {} describe('isAnyOf', () => { it('returns true only if any matchers match (match function)', () => { diff --git a/yarn.lock b/yarn.lock index e50364111f..3c59dcdb91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7481,7 +7481,7 @@ __metadata: prettier: "npm:^3.2.5" query-string: "npm:^7.0.1" redux: "npm:^5.0.1" - redux-thunk: "npm:^3.1.0" + redux-thunk: "https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz" reselect: "npm:^5.1.0" rimraf: "npm:^3.0.2" size-limit: "npm:^11.0.1" @@ -24680,6 +24680,15 @@ __metadata: languageName: node linkType: hard +"redux-thunk@https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz": + version: 3.1.0 + resolution: "redux-thunk@https://pkg.csb.dev/reduxjs/redux-thunk/commit/fdb2e154/redux-thunk/_pkg.tgz" + peerDependencies: + redux: ^5.0.0 + checksum: 10/c47c69d216747dbb6294239a76ca4b9a3ce82b0bb0272213f7bec7e8b4abbfb334a1a2dfe2211fa2202742f5969795e065ba0f6cf6dd128e351851d5fababb2c + languageName: node + linkType: hard + "redux-thunk@npm:^2.4.1": version: 2.4.1 resolution: "redux-thunk@npm:2.4.1" @@ -24698,15 +24707,6 @@ __metadata: languageName: node linkType: hard -"redux-thunk@npm:^3.1.0": - version: 3.1.0 - resolution: "redux-thunk@npm:3.1.0" - peerDependencies: - redux: ^5.0.0 - checksum: 10/38c563db5f0bbec90d2e65cc27f3c870c1b6102e0c071258734fac41cb0e51d31d894125815c2f4133b20aff231f51f028ad99bccc05a7e3249f1a5d5a959ed3 - languageName: node - linkType: hard - "redux@npm:^4.1.2": version: 4.1.2 resolution: "redux@npm:4.1.2"