Skip to content

Commit e3e8c2d

Browse files
author
ben.durrant
committed
JSDoc
1 parent af875ae commit e3e8c2d

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface Slice<
3939
State = any,
4040
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
4141
Name extends string = string,
42-
Selectors extends SliceSelectors<State> = {}
42+
Selectors extends SliceSelectors<State> = SliceSelectors<State>
4343
> {
4444
/**
4545
* The slice name.
@@ -69,51 +69,70 @@ export interface Slice<
6969
*/
7070
getInitialState: () => State
7171

72+
/**
73+
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
74+
*/
7275
getSelectors(): Id<SliceDefinedSelectors<State, Selectors, State>>
7376

77+
/**
78+
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
79+
*/
7480
getSelectors<RootState>(
7581
selectState: (rootState: RootState) => State
7682
): Id<SliceDefinedSelectors<State, Selectors, RootState>>
7783

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+
*/
7889
selectors: Id<SliceDefinedSelectors<State, Selectors, { [K in Name]: State }>>
7990

91+
/**
92+
* Inject slice into provided reducer (return value from `combineSlices`), and return injected slice.
93+
*/
8094
injectInto(
8195
combinedReducer: CombinedSliceReducer<any>,
8296
config?: InjectConfig & { name?: string }
8397
): InjectedSlice<State, CaseReducers, Name, Selectors>
8498
}
8599

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+
*/
86105
interface InjectedSlice<
87106
State = any,
88107
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
89108
Name extends string = string,
90-
Selectors extends SliceSelectors<State> = {}
109+
Selectors extends SliceSelectors<State> = SliceSelectors<State>
91110
> extends Omit<
92111
Slice<State, CaseReducers, Name, Selectors>,
93112
'getSelectors' | 'selectors'
94113
> {
114+
/**
115+
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
116+
*/
95117
getSelectors(): Id<SliceDefinedSelectors<State, Selectors, State | undefined>>
96118

119+
/**
120+
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
121+
*/
97122
getSelectors<RootState>(
98123
selectState: (rootState: RootState) => State | undefined
99124
): Id<SliceDefinedSelectors<State, Selectors, RootState>>
100125

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+
*/
101131
selectors: Id<
102132
SliceDefinedSelectors<State, Selectors, { [K in Name]?: State | undefined }>
103133
>
104134
}
105135

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-
117136
/**
118137
* Options for `createSlice()`.
119138
*
@@ -123,7 +142,7 @@ export interface CreateSliceOptions<
123142
State = any,
124143
CR extends SliceCaseReducers<State> = SliceCaseReducers<State>,
125144
Name extends string = string,
126-
Selectors extends SliceSelectors<State> = Record<never, never>
145+
Selectors extends SliceSelectors<State> = SliceSelectors<State>
127146
> {
128147
/**
129148
* The slice's name. Used to namespace the generated action types.
@@ -189,6 +208,9 @@ createSlice({
189208
*/
190209
extraReducers?: (builder: ActionReducerMapBuilder<NoInfer<State>>) => void
191210

211+
/**
212+
* A map of selectors that receive the slice's state and any additional arguments, and return a result.
213+
*/
192214
selectors?: Selectors
193215
}
194216

@@ -213,6 +235,9 @@ export type SliceCaseReducers<State> = {
213235
| CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>
214236
}
215237

238+
/**
239+
* The type describing a slice's `selectors` option.
240+
*/
216241
export type SliceSelectors<State> = {
217242
[K: string]: (sliceState: State, ...args: any[]) => any
218243
}
@@ -280,6 +305,22 @@ type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
280305
: CaseReducers[Type]
281306
}
282307

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+
283324
/**
284325
* Used on a SliceCaseReducers object.
285326
* Ensures that if a CaseReducer is a `CaseReducerWithPrepare`, that

0 commit comments

Comments
 (0)