1
- import { Action , AnyAction , ActionCreator , Reducer } from 'redux'
1
+ import { Action , AnyAction , Reducer } from 'redux'
2
2
import { createAction , PayloadAction } from './createAction'
3
3
import { createReducer , CaseReducersMapObject } from './createReducer'
4
4
import { createSliceSelector , createSelectorName } from './sliceSelector'
5
5
6
+ /**
7
+ * An action creator atttached to a slice.
8
+ */
9
+ export type SliceActionCreator < P > = ( payload : P ) => PayloadAction < P >
10
+
6
11
export interface Slice <
7
12
S = any ,
8
13
A extends Action = AnyAction ,
9
- AT extends string = string
14
+ AP extends { [ key : string ] : any } = { [ key : string ] : any }
10
15
> {
11
16
/**
12
17
* The slice name.
@@ -22,7 +27,7 @@ export interface Slice<
22
27
* Action creators for the types of actions that are handled by the slice
23
28
* reducer.
24
29
*/
25
- actions : { [ type in AT ] : ActionCreator < A > }
30
+ actions : { [ type in keyof AP ] : SliceActionCreator < AP [ type ] > }
26
31
27
32
/**
28
33
* Selectors for the slice reducer state. `createSlice()` inserts a single
@@ -68,6 +73,18 @@ export interface CreateSliceOptions<
68
73
extraReducers ?: CR2
69
74
}
70
75
76
+ type ExtractPayloads <
77
+ S ,
78
+ A extends PayloadAction ,
79
+ CR extends CaseReducersMapObject < S , A >
80
+ > = {
81
+ [ type in keyof CR ] : CR [ type ] extends ( state : S ) => any
82
+ ? void
83
+ : ( CR [ type ] extends ( state : S , action : PayloadAction < infer P > ) => any
84
+ ? P
85
+ : never )
86
+ }
87
+
71
88
function getType ( slice : string , actionKey : string ) : string {
72
89
return slice ? `${ slice } /${ actionKey } ` : actionKey
73
90
}
@@ -82,11 +99,11 @@ function getType(slice: string, actionKey: string): string {
82
99
*/
83
100
export function createSlice <
84
101
S = any ,
85
- A extends PayloadAction = PayloadAction ,
102
+ A extends PayloadAction = PayloadAction < any > ,
86
103
CR extends CaseReducersMapObject < S , A > = CaseReducersMapObject < S , A >
87
104
> (
88
105
options : CreateSliceOptions < S , A , CR >
89
- ) : Slice < S , A , Extract < keyof CR , string > > {
106
+ ) : Slice < S , A , ExtractPayloads < S , A , CR > > {
90
107
const { slice = '' , initialState } = options
91
108
const reducers = options . reducers || { }
92
109
const extraReducers = options . extraReducers || { }
0 commit comments