Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code to match thunk rework #4486

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 15 additions & 14 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -239,19 +239,20 @@ export type AsyncThunkAction<
Returned,
ThunkArg,
ThunkApiConfig extends AsyncThunkConfig,
> = (
dispatch: NonNullable<GetDispatch<ThunkApiConfig>>,
getState: () => GetState<ThunkApiConfig>,
extra: GetExtra<ThunkApiConfig>,
) => SafePromise<
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>>
| ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>
> & {
abort: (reason?: string) => void
requestId: string
arg: ThunkArg
unwrap: () => Promise<Returned>
}
> = ThunkAction<
NonNullable<GetDispatch<ThunkApiConfig>>,
GetState<ThunkApiConfig>,
GetExtra<ThunkApiConfig>,
SafePromise<
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>>
| ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>
> & {
abort: (reason?: string) => void
requestId: string
arg: ThunkArg
unwrap: () => Promise<Returned>
}
>

type AsyncThunkActionCreator<
Returned,
Expand Down
7 changes: 6 additions & 1 deletion packages/toolkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ type AppStore = typeof store
type AppDispatch = typeof store.dispatch
type RootState = ReturnType<typeof store.getState>
type AppThunk<ThunkReturnType = void> = ThunkAction<
ThunkReturnType,
AppDispatch,
RootState,
unknown,
Action
ThunkReturnType
>

describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ type AppStore = typeof store
type AppDispatch = typeof store.dispatch
type RootState = ReturnType<typeof store.getState>
type AppThunk<ThunkReturnType = void> = ThunkAction<
ThunkReturnType,
AppDispatch,
RootState,
unknown,
Action
ThunkReturnType
>

const listenerMiddleware = createListenerMiddleware()
Expand Down
15 changes: 13 additions & 2 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
UnknownAction,
ThunkAction,
SerializedError,
ThunkDispatch,
} from '@reduxjs/toolkit'
import type { SubscriptionOptions, RootState } from './apiState'
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
Expand Down Expand Up @@ -58,7 +59,12 @@ type StartQueryActionCreator<
> = (
arg: QueryArgFrom<D>,
options?: StartQueryActionCreatorOptions,
) => ThunkAction<QueryActionCreatorResult<D>, any, any, UnknownAction>
) => ThunkAction<
ThunkDispatch<any, any, UnknownAction>,
any,
any,
QueryActionCreatorResult<D>
>

export type QueryActionCreatorResult<
D extends QueryDefinition<any, any, any, any>,
Expand Down Expand Up @@ -88,7 +94,12 @@ type StartMutationActionCreator<
track?: boolean
fixedCacheKey?: string
},
) => ThunkAction<MutationActionCreatorResult<D>, any, any, UnknownAction>
) => ThunkAction<
ThunkDispatch<any, any, UnknownAction>,
any,
any,
MutationActionCreatorResult<D>
>

export type MutationActionCreatorResult<
D extends MutationDefinition<any, any, any, any>,
Expand Down
24 changes: 17 additions & 7 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ export type PatchQueryDataThunk<
args: QueryArgFrom<Definitions[EndpointName]>,
patches: readonly Patch[],
updateProvided?: boolean,
) => ThunkAction<void, PartialState, any, UnknownAction>
) => ThunkAction<
ThunkDispatch<PartialState, any, UnknownAction>,
PartialState,
any,
void
>

export type UpdateQueryDataThunk<
Definitions extends EndpointDefinitions,
Expand All @@ -178,7 +183,12 @@ export type UpdateQueryDataThunk<
args: QueryArgFrom<Definitions[EndpointName]>,
updateRecipe: Recipe<ResultTypeFrom<Definitions[EndpointName]>>,
updateProvided?: boolean,
) => ThunkAction<PatchCollection, PartialState, any, UnknownAction>
) => ThunkAction<
ThunkDispatch<PartialState, any, UnknownAction>,
PartialState,
any,
PatchCollection
>

export type UpsertQueryDataThunk<
Definitions extends EndpointDefinitions,
Expand All @@ -188,14 +198,14 @@ export type UpsertQueryDataThunk<
args: QueryArgFrom<Definitions[EndpointName]>,
value: ResultTypeFrom<Definitions[EndpointName]>,
) => ThunkAction<
ThunkDispatch<PartialState, any, UnknownAction>,
PartialState,
any,
QueryActionCreatorResult<
Definitions[EndpointName] extends QueryDefinition<any, any, any, any>
? Definitions[EndpointName]
: never
>,
PartialState,
any,
UnknownAction
>
>

/**
Expand Down Expand Up @@ -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<void, any, any, UnknownAction> =>
): ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, void> =>
(dispatch: ThunkDispatch<any, any, any>, getState: () => any) => {
const force = hasTheForce(options) && options.force
const maxAge = hasMaxAge(options) && options.ifOlderThan
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export type CoreModule =
| ReferenceCacheCollection

export interface ThunkWithReturnValue<T>
extends ThunkAction<T, any, any, UnknownAction> {}
extends ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, T> {}

declare module '../apiTypes' {
export interface ApiModules<
Expand Down Expand Up @@ -222,7 +222,7 @@ declare module '../apiTypes' {
endpointName: EndpointName,
arg: QueryArgFrom<Definitions[EndpointName]>,
options: PrefetchOptions,
): ThunkAction<void, any, any, UnknownAction>
): ThunkAction<ThunkDispatch<any, any, UnknownAction>, 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.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ type GenericPrefetchThunk = (
endpointName: any,
arg: any,
options: PrefetchOptions,
) => ThunkAction<void, any, any, UnknownAction>
) => ThunkAction<ThunkDispatch<any, any, UnknownAction>, any, any, void>

/**
*
Expand Down
34 changes: 18 additions & 16 deletions packages/toolkit/src/tests/configureStore.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
ThunkAction,
ThunkDispatch,
ThunkMiddleware,
ThunkOverload,
UnknownAction,
} from '@reduxjs/toolkit'
import {
Expand Down Expand Up @@ -150,9 +151,7 @@ describe('type tests', () => {
enhancers: () => [enhancer],
})

expectTypeOf(store.dispatch).toMatchTypeOf<
Dispatch & ThunkDispatch<number, undefined, UnknownAction>
>()
expectTypeOf(store.dispatch).toMatchTypeOf<Dispatch>()

configureStore({
reducer: () => 0,
Expand Down Expand Up @@ -202,9 +201,7 @@ describe('type tests', () => {
.concat(somePropertyStoreEnhancer),
})

expectTypeOf(store3.dispatch).toMatchTypeOf<
Dispatch & ThunkDispatch<number, undefined, UnknownAction>
>()
expectTypeOf(store3.dispatch).toMatchTypeOf<Dispatch>()

expectTypeOf(store3.someProperty).toBeString()

Expand Down Expand Up @@ -423,7 +420,12 @@ describe('type tests', () => {
type StateA = number
const reducerA = () => 0
const thunkA = () => {
return (() => {}) as any as ThunkAction<Promise<'A'>, StateA, any, any>
return (() => {}) as any as ThunkAction<
ThunkDispatch<StateA, any, any>,
StateA,
any,
Promise<'A'>
>
}

type StateB = string
Expand Down Expand Up @@ -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
>)
})

Expand Down Expand Up @@ -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<CounterState, undefined, UnknownAction> &
ThunkOverload<CounterState, undefined> &
Dispatch<UnknownAction>
>()

Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/tests/getDefaultMiddleware.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/toolkit/src/tests/getDefaultMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
Action,
Middleware,
ThunkAction,
ThunkDispatch,
UnknownAction,
} from '@reduxjs/toolkit'
import { configureStore } from '@reduxjs/toolkit'
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/tests/matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
createReducer,
} from '@reduxjs/toolkit'

const thunk: ThunkAction<any, any, any, UnknownAction> = () => {}
const thunk: ThunkAction<any, any, any, any> = () => {}

describe('isAnyOf', () => {
it('returns true only if any matchers match (match function)', () => {
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
Loading