1- import { Action , AnyAction , ActionCreator , Reducer } from 'redux'
1+ import { Action , AnyAction , Reducer } from 'redux'
22import { createAction , PayloadAction } from './createAction'
33import { createReducer , CaseReducersMapObject } from './createReducer'
44import { createSliceSelector , createSelectorName } from './sliceSelector'
55
6+ /**
7+ * An action creator atttached to a slice.
8+ */
9+ export type SliceActionCreator < P > = ( payload : P ) => PayloadAction < P >
10+
611export interface Slice <
712 S = any ,
813 A extends Action = AnyAction ,
9- AT extends string = string
14+ AP extends { [ key : string ] : any } = { [ key : string ] : any }
1015> {
1116 /**
1217 * The slice name.
@@ -22,7 +27,7 @@ export interface Slice<
2227 * Action creators for the types of actions that are handled by the slice
2328 * reducer.
2429 */
25- actions : { [ type in AT ] : ActionCreator < A > }
30+ actions : { [ type in keyof AP ] : SliceActionCreator < AP [ type ] > }
2631
2732 /**
2833 * Selectors for the slice reducer state. `createSlice()` inserts a single
@@ -68,6 +73,18 @@ export interface CreateSliceOptions<
6873 extraReducers ?: CR2
6974}
7075
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+
7188function getType ( slice : string , actionKey : string ) : string {
7289 return slice ? `${ slice } /${ actionKey } ` : actionKey
7390}
@@ -82,11 +99,11 @@ function getType(slice: string, actionKey: string): string {
8299 */
83100export function createSlice <
84101 S = any ,
85- A extends PayloadAction = PayloadAction ,
102+ A extends PayloadAction = PayloadAction < any > ,
86103 CR extends CaseReducersMapObject < S , A > = CaseReducersMapObject < S , A >
87104> (
88105 options : CreateSliceOptions < S , A , CR >
89- ) : Slice < S , A , Extract < keyof CR , string > > {
106+ ) : Slice < S , A , ExtractPayloads < S , A , CR > > {
90107 const { slice = '' , initialState } = options
91108 const reducers = options . reducers || { }
92109 const extraReducers = options . extraReducers || { }
0 commit comments