@@ -39,7 +39,7 @@ export interface Slice<
39
39
State = any ,
40
40
CaseReducers extends SliceCaseReducers < State > = SliceCaseReducers < State > ,
41
41
Name extends string = string ,
42
- Selectors extends SliceSelectors < State > = { }
42
+ Selectors extends SliceSelectors < State > = SliceSelectors < State >
43
43
> {
44
44
/**
45
45
* The slice name.
@@ -69,51 +69,70 @@ export interface Slice<
69
69
*/
70
70
getInitialState : ( ) => State
71
71
72
+ /**
73
+ * Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
74
+ */
72
75
getSelectors ( ) : Id < SliceDefinedSelectors < State , Selectors , State > >
73
76
77
+ /**
78
+ * Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
79
+ */
74
80
getSelectors < RootState > (
75
81
selectState : ( rootState : RootState ) => State
76
82
) : Id < SliceDefinedSelectors < State , Selectors , RootState > >
77
83
84
+ /**
85
+ * Selectors that assume the slice's state is `rootState[slice.name]` (which is usually the case)
86
+ *
87
+ * Equivalent to `slice.getSelectors((state: RootState) => state[slice.name])`.
88
+ */
78
89
selectors : Id < SliceDefinedSelectors < State , Selectors , { [ K in Name ] : State } > >
79
90
91
+ /**
92
+ * Inject slice into provided reducer (return value from `combineSlices`), and return injected slice.
93
+ */
80
94
injectInto (
81
95
combinedReducer : CombinedSliceReducer < any > ,
82
96
config ?: InjectConfig & { name ?: string }
83
97
) : InjectedSlice < State , CaseReducers , Name , Selectors >
84
98
}
85
99
100
+ /**
101
+ * A slice after being called with `injectInto(reducer)`.
102
+ *
103
+ * Selectors can now be called with an `undefined` value, in which case they use the slice's initial state.
104
+ */
86
105
interface InjectedSlice <
87
106
State = any ,
88
107
CaseReducers extends SliceCaseReducers < State > = SliceCaseReducers < State > ,
89
108
Name extends string = string ,
90
- Selectors extends SliceSelectors < State > = { }
109
+ Selectors extends SliceSelectors < State > = SliceSelectors < State >
91
110
> extends Omit <
92
111
Slice < State , CaseReducers , Name , Selectors > ,
93
112
'getSelectors' | 'selectors'
94
113
> {
114
+ /**
115
+ * Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
116
+ */
95
117
getSelectors ( ) : Id < SliceDefinedSelectors < State , Selectors , State | undefined > >
96
118
119
+ /**
120
+ * Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
121
+ */
97
122
getSelectors < RootState > (
98
123
selectState : ( rootState : RootState ) => State | undefined
99
124
) : Id < SliceDefinedSelectors < State , Selectors , RootState > >
100
125
126
+ /**
127
+ * Selectors that assume the slice's state is `rootState[slice.name]` (which is usually the case)
128
+ *
129
+ * Equivalent to `slice.getSelectors((state: RootState) => state[slice.name])`.
130
+ */
101
131
selectors : Id <
102
132
SliceDefinedSelectors < State , Selectors , { [ K in Name ] ?: State | undefined } >
103
133
>
104
134
}
105
135
106
- type SliceDefinedSelectors <
107
- State ,
108
- Selectors extends SliceSelectors < State > ,
109
- RootState
110
- > = {
111
- [ K in keyof Selectors as [ string ] extends [ K ] ? never : K ] : (
112
- rootState : RootState ,
113
- ...args : Tail < Parameters < Selectors [ K ] > >
114
- ) => ReturnType < Selectors [ K ] >
115
- }
116
-
117
136
/**
118
137
* Options for `createSlice()`.
119
138
*
@@ -123,7 +142,7 @@ export interface CreateSliceOptions<
123
142
State = any ,
124
143
CR extends SliceCaseReducers < State > = SliceCaseReducers < State > ,
125
144
Name extends string = string ,
126
- Selectors extends SliceSelectors < State > = Record < never , never >
145
+ Selectors extends SliceSelectors < State > = SliceSelectors < State >
127
146
> {
128
147
/**
129
148
* The slice's name. Used to namespace the generated action types.
@@ -189,6 +208,9 @@ createSlice({
189
208
*/
190
209
extraReducers ?: ( builder : ActionReducerMapBuilder < NoInfer < State > > ) => void
191
210
211
+ /**
212
+ * A map of selectors that receive the slice's state and any additional arguments, and return a result.
213
+ */
192
214
selectors ?: Selectors
193
215
}
194
216
@@ -213,6 +235,9 @@ export type SliceCaseReducers<State> = {
213
235
| CaseReducerWithPrepare < State , PayloadAction < any , string , any , any > >
214
236
}
215
237
238
+ /**
239
+ * The type describing a slice's `selectors` option.
240
+ */
216
241
export type SliceSelectors < State > = {
217
242
[ K : string ] : ( sliceState : State , ...args : any [ ] ) => any
218
243
}
@@ -280,6 +305,22 @@ type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
280
305
: CaseReducers [ Type ]
281
306
}
282
307
308
+ /**
309
+ * Extracts the final selector type from the `selectors` object.
310
+ *
311
+ * Removes the `string` index signature from the default value.
312
+ */
313
+ type SliceDefinedSelectors <
314
+ State ,
315
+ Selectors extends SliceSelectors < State > ,
316
+ RootState
317
+ > = {
318
+ [ K in keyof Selectors as string extends K ? never : K ] : (
319
+ rootState : RootState ,
320
+ ...args : Tail < Parameters < Selectors [ K ] > >
321
+ ) => ReturnType < Selectors [ K ] >
322
+ }
323
+
283
324
/**
284
325
* Used on a SliceCaseReducers object.
285
326
* Ensures that if a CaseReducer is a `CaseReducerWithPrepare`, that
0 commit comments