1- import { Dispatch } from 'redux'
1+ import { Dispatch , AnyAction } from 'redux'
22import nanoid from 'nanoid'
33import {
44 createAction ,
55 PayloadAction ,
66 ActionCreatorWithPreparedPayload
77} from './createAction'
8+ import { ThunkDispatch } from 'redux-thunk'
9+ import { FallbackIfUnknown } from './tsHelpers'
810
911// @ts -ignore we need the import of these types due to a bundling issue.
1012type _Keep = PayloadAction | ActionCreatorWithPreparedPayload < any , unknown >
@@ -50,6 +52,39 @@ export const miniSerializeError = (value: any): any => {
5052 return value
5153}
5254
55+ type AsyncThunkConfig = {
56+ state ?: unknown
57+ dispatch ?: Dispatch
58+ extra ?: unknown
59+ }
60+
61+ type GetState < ThunkApiConfig > = ThunkApiConfig extends {
62+ state : infer State
63+ }
64+ ? State
65+ : unknown
66+ type GetExtra < ThunkApiConfig > = ThunkApiConfig extends { extra : infer Extra }
67+ ? Extra
68+ : unknown
69+ type GetDispatch < ThunkApiConfig > = ThunkApiConfig extends {
70+ dispatch : infer Dispatch
71+ }
72+ ? FallbackIfUnknown <
73+ Dispatch ,
74+ ThunkDispatch <
75+ GetState < ThunkApiConfig > ,
76+ GetExtra < ThunkApiConfig > ,
77+ AnyAction
78+ >
79+ >
80+ : ThunkDispatch < GetState < ThunkApiConfig > , GetExtra < ThunkApiConfig > , AnyAction >
81+
82+ type GetThunkAPI < ThunkApiConfig > = BaseThunkAPI <
83+ GetState < ThunkApiConfig > ,
84+ GetExtra < ThunkApiConfig > ,
85+ GetDispatch < ThunkApiConfig >
86+ >
87+
5388/**
5489 *
5590 * @param type
@@ -60,20 +95,12 @@ export const miniSerializeError = (value: any): any => {
6095export function createAsyncThunk <
6196 Returned ,
6297 ThunkArg = void ,
63- State = unknown ,
64- Extra = unknown ,
65- DispatchType extends Dispatch = Dispatch ,
66- ActionType extends string = string ,
67- ThunkAPI extends BaseThunkAPI < any , any , any > = BaseThunkAPI <
68- State ,
69- Extra ,
70- DispatchType
71- >
98+ ThunkApiConfig extends AsyncThunkConfig = { }
7299> (
73- type : ActionType ,
100+ type : string ,
74101 payloadCreator : (
75102 arg : ThunkArg ,
76- thunkAPI : ThunkAPI
103+ thunkAPI : GetThunkAPI < ThunkApiConfig >
77104 ) => Promise < Returned > | Returned
78105) {
79106 const fulfilled = createAction (
@@ -115,9 +142,9 @@ export function createAsyncThunk<
115142
116143 function actionCreator ( arg : ThunkArg ) {
117144 return (
118- dispatch : ThunkAPI [ 'dispatch' ] ,
119- getState : ThunkAPI [ 'getState' ] ,
120- extra : ThunkAPI [ 'extra' ]
145+ dispatch : GetDispatch < ThunkApiConfig > ,
146+ getState : ( ) => GetState < ThunkApiConfig > ,
147+ extra : GetExtra < ThunkApiConfig >
121148 ) => {
122149 const requestId = nanoid ( )
123150 const abortController = new AbortController ( )
@@ -145,7 +172,7 @@ export function createAsyncThunk<
145172 extra,
146173 requestId,
147174 signal : abortController . signal
148- } as ThunkAPI ) ,
175+ } ) ,
149176 requestId ,
150177 arg
151178 )
0 commit comments