Skip to content

Commit fa83273

Browse files
committed
curry createAsyncThunk
1 parent 038b32c commit fa83273

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/createAsyncThunk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const miniSerializeError = (value: any): SerializedError => {
6262
return { message: String(value) }
6363
}
6464

65-
type AsyncThunkConfig = {
65+
export type AsyncThunkConfig = {
6666
state?: unknown
6767
dispatch?: Dispatch
6868
extra?: unknown
@@ -191,7 +191,7 @@ type AsyncThunkActionCreator<
191191
: (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>
192192
>
193193

194-
interface AsyncThunkOptions<
194+
export interface AsyncThunkOptions<
195195
ThunkArg = void,
196196
ThunkApiConfig extends AsyncThunkConfig = {}
197197
> {

src/curriedTypes/createAsyncThunk.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export interface CurryableTypes {
2+
createAsyncThunk: typeof import('../createAsyncThunk').createAsyncThunk
3+
}
4+
5+
export interface CurriedType<RootState, Dispatch> {
6+
createAsyncThunk: CuriedCreateAsyncThunk<RootState, Dispatch>
7+
}
8+
9+
/* eslint-disable import/first */
10+
import {
11+
AsyncThunk,
12+
AsyncThunkPayloadCreator,
13+
AsyncThunkConfig,
14+
AsyncThunkOptions
15+
} from '../createAsyncThunk'
16+
17+
type CuriedCreateAsyncThunk<RootState, Dispatch> = <
18+
Returned,
19+
ThunkArg = void,
20+
ThunkApiConfig extends Omit<AsyncThunkConfig, 'dispatch' | 'state'> = {}
21+
>(
22+
typePrefix: string,
23+
payloadCreator: AsyncThunkPayloadCreator<
24+
Returned,
25+
ThunkArg,
26+
ThunkApiConfig & { dispatch: Dispatch; state: RootState }
27+
>,
28+
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>
29+
) => AsyncThunk<Returned, ThunkArg, ThunkApiConfig>

src/curriedTypes/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import {
22
CurriedType as RRCurriedType,
33
CurryableTypes as RRCurryableType
44
} from './react-redux'
5+
import {
6+
CurriedType as CatCurriedType,
7+
CurryableTypes as CatCurryableType
8+
} from './createAsyncThunk'
9+
510
import { UnionToIntersection } from '../tsHelpers'
611

712
type CurrySingleType<RootState, Dispatch> = UnionToIntersection<
@@ -25,7 +30,8 @@ export type CurryType<RootState, Dispatch> = CurrySingleType<
2530
> &
2631
CurryMultipleTypes<RootState, Dispatch>
2732

28-
export interface CurryableTypes extends RRCurryableType {}
33+
export interface CurryableTypes extends RRCurryableType, CatCurryableType {}
2934

3035
export interface CurriedType<RootState, Dispatch>
31-
extends RRCurriedType<RootState, Dispatch> {}
36+
extends RRCurriedType<RootState, Dispatch>,
37+
CatCurriedType<RootState, Dispatch> {}

type-tests/files/curriedTypes.typetest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useDispatch, useSelector } from 'react-redux'
2-
import { configureStore } from 'src'
2+
import { configureStore, createAsyncThunk } from 'src'
33

44
type DispatchType = typeof store.dispatch
55
type StateType = ReturnType<typeof store.getState>
@@ -60,3 +60,11 @@ function expectType<T>(t: T) {
6060
connectAdvanced
6161
} = store.withCurriedTypes(require('react-redux'))
6262
}
63+
64+
{
65+
const curriedCAT = store.withCurriedTypes(createAsyncThunk)
66+
curriedCAT('foo', (arg: any, { getState, dispatch }) => {
67+
expectType<DispatchType>(dispatch)
68+
expectType<StateType>(getState())
69+
})
70+
}

0 commit comments

Comments
 (0)