1
- import { Action , AnyAction , Reducer } from 'redux'
1
+ import { Reducer } from 'redux'
2
2
import { createAction , PayloadAction } from './createAction'
3
- import { createReducer , CaseReducersMapObject } from './createReducer'
3
+ import { createReducer , CaseReducers } from './createReducer'
4
4
import { createSliceSelector , createSelectorName } from './sliceSelector'
5
5
6
6
/**
7
7
* An action creator atttached to a slice.
8
8
*/
9
- export type SliceActionCreator < P > = ( payload : P ) => PayloadAction < P >
9
+ export type SliceActionCreator < P > = P extends void
10
+ ? ( ) => PayloadAction < void >
11
+ : ( payload : P ) => PayloadAction < P >
10
12
11
13
export interface Slice <
12
14
S = any ,
13
- A extends Action = AnyAction ,
14
15
AP extends { [ key : string ] : any } = { [ key : string ] : any }
15
16
> {
16
17
/**
@@ -21,7 +22,7 @@ export interface Slice<
21
22
/**
22
23
* The slice's reducer.
23
24
*/
24
- reducer : Reducer < S , A >
25
+ reducer : Reducer < S >
25
26
26
27
/**
27
28
* Action creators for the types of actions that are handled by the slice
@@ -43,9 +44,7 @@ export interface Slice<
43
44
*/
44
45
export interface CreateSliceOptions <
45
46
S = any ,
46
- A extends Action = AnyAction ,
47
- CR extends CaseReducersMapObject < S , A > = CaseReducersMapObject < S , A > ,
48
- CR2 extends CaseReducersMapObject < S , A > = CaseReducersMapObject < S , A >
47
+ CR extends CaseReducers < S , any > = CaseReducers < S , any >
49
48
> {
50
49
/**
51
50
* The slice's name. Used to namespace the generated action types and to
@@ -70,19 +69,15 @@ export interface CreateSliceOptions<
70
69
* functions. These reducers should have existing action types used
71
70
* as the keys, and action creators will _not_ be generated.
72
71
*/
73
- extraReducers ?: CR2
72
+ extraReducers ?: CaseReducers < S , any >
74
73
}
75
74
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
75
+ type CaseReducerActionPayloads < CR extends CaseReducers < any , any > > = {
76
+ [ T in keyof CR ] : CR [ T ] extends ( state : any ) => any
82
77
? void
83
- : ( CR [ type ] extends ( state : S , action : PayloadAction < infer P > ) => any
78
+ : ( CR [ T ] extends ( state : any , action : PayloadAction < infer P > ) => any
84
79
? P
85
- : never )
80
+ : void )
86
81
}
87
82
88
83
function getType ( slice : string , actionKey : string ) : string {
@@ -97,13 +92,9 @@ function getType(slice: string, actionKey: string): string {
97
92
*
98
93
* The `reducer` argument is passed to `createReducer()`.
99
94
*/
100
- export function createSlice <
101
- S = any ,
102
- A extends PayloadAction = PayloadAction < any > ,
103
- CR extends CaseReducersMapObject < S , A > = CaseReducersMapObject < S , A >
104
- > (
105
- options : CreateSliceOptions < S , A , CR >
106
- ) : Slice < S , A , ExtractPayloads < S , A , CR > > {
95
+ export function createSlice < S , CR extends CaseReducers < S , any > > (
96
+ options : CreateSliceOptions < S , CR >
97
+ ) : Slice < S , CaseReducerActionPayloads < CR > > {
107
98
const { slice = '' , initialState } = options
108
99
const reducers = options . reducers || { }
109
100
const extraReducers = options . extraReducers || { }
0 commit comments