Skip to content

Commit bfc9bae

Browse files
committed
move reducerPath to reducerDetails
1 parent 2cb7e47 commit bfc9bae

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,7 @@ interface InternalReducerHandlingContext<State> {
210210
actionCreators: Record<string, any>
211211
}
212212

213-
export interface ReducerHandlingContext<
214-
State,
215-
Name extends string = string,
216-
ReducerPath extends string = Name,
217-
> {
213+
export interface ReducerHandlingContext<State> {
218214
/**
219215
* Adds a case reducer to handle a single action type.
220216
* @param actionCreator - Either a plain action type string, or an action creator generated by [`createAction`](./createAction) that can be used to determine the action type.
@@ -223,7 +219,7 @@ export interface ReducerHandlingContext<
223219
addCase<ActionCreator extends TypedActionCreator<string>>(
224220
actionCreator: ActionCreator,
225221
reducer: CaseReducer<State, ReturnType<ActionCreator>>,
226-
): ReducerHandlingContext<State, Name, ReducerPath>
222+
): ReducerHandlingContext<State>
227223
/**
228224
* Adds a case reducer to handle a single action type.
229225
* @param actionCreator - Either a plain action type string, or an action creator generated by [`createAction`](./createAction) that can be used to determine the action type.
@@ -232,7 +228,7 @@ export interface ReducerHandlingContext<
232228
addCase<Type extends string, A extends Action<Type>>(
233229
type: Type,
234230
reducer: CaseReducer<State, A>,
235-
): ReducerHandlingContext<State, Name, ReducerPath>
231+
): ReducerHandlingContext<State>
236232

237233
/**
238234
* Allows you to match incoming actions against your own filter function instead of only the `action.type` property.
@@ -248,7 +244,7 @@ export interface ReducerHandlingContext<
248244
addMatcher<A>(
249245
matcher: TypeGuard<A>,
250246
reducer: CaseReducer<State, A extends Action ? A : A & Action>,
251-
): ReducerHandlingContext<State, Name, ReducerPath>
247+
): ReducerHandlingContext<State>
252248
/**
253249
* Add an action to be exposed under the final `slice.actions[reducerName]` key.
254250
*
@@ -262,9 +258,7 @@ export interface ReducerHandlingContext<
262258
*
263259
* dispatch(addPost(post))
264260
*/
265-
exposeAction(
266-
actionCreator: unknown,
267-
): ReducerHandlingContext<State, Name, ReducerPath>
261+
exposeAction(actionCreator: unknown): ReducerHandlingContext<State>
268262

269263
/**
270264
* Add a case reducer to be exposed under the final `slice.caseReducers[reducerName]` key.
@@ -279,39 +273,28 @@ export interface ReducerHandlingContext<
279273
*
280274
* slice.caseReducers.addPost([], addPost(post))
281275
*/
282-
exposeCaseReducer(
283-
reducer: unknown,
284-
): ReducerHandlingContext<State, Name, ReducerPath>
276+
exposeCaseReducer(reducer: unknown): ReducerHandlingContext<State>
285277

286278
/**
287279
* Provides access to the initial state value given to the slice.
288280
* If a lazy state initializer was provided, it will be called and a fresh value returned.
289281
*/
290282
getInitialState(): State
291283

292-
/**
293-
* The `name` option provided for the slice.
294-
*/
295-
name: Name
296-
297-
/**
298-
* The `reducerPath` option provided for the slice.
299-
* Defaults to `name` if not provided.
300-
*/
301-
reducerPath: ReducerPath
302-
303284
/**
304285
* Tries to select the slice's state from a possible root state shape, using `reducerPath`.
305286
* Throws an error if slice's state is not found.
306287
*
307288
* *Note that only the original `reducerPath` option is used - if a different `reducerPath` is used when injecting, this will not be reflected.*
308289
*/
309-
selectSlice(state: Record<ReducerPath, State>): State
290+
selectSlice(state: Record<string, State>): State
310291
}
311292

312293
export interface ReducerDetails {
313294
/** The name of the slice */
314295
sliceName: string
296+
/** The reducerPath option passed for the slice. Defaults to `sliceName` if not provided. */
297+
reducerPath: string
315298
/** The key the reducer was defined under */
316299
reducerName: string
317300
/** The predefined action type, i.e. `${sliceName}/${reducerName}` */
@@ -367,10 +350,10 @@ export type ReducerCreator<Type extends RegisteredReducerType> = {
367350
} & (ReducerDefinitionsForType<Type> extends never
368351
? {}
369352
: {
370-
handle<State, Name extends string, ReducerPath extends string>(
353+
handle<State>(
371354
details: ReducerDetails,
372355
definition: ReducerDefinitionsForType<Type>,
373-
context: ReducerHandlingContext<State, Name, ReducerPath>,
356+
context: ReducerHandlingContext<State>,
374357
): void
375358
})
376359

@@ -966,7 +949,7 @@ export function buildCreateSlice<
966949
}
967950

968951
function getContext({ reducerName }: ReducerDetails) {
969-
const context: ReducerHandlingContext<State, Name, ReducerPath> = {
952+
const context: ReducerHandlingContext<State> = {
970953
addCase(
971954
typeOrActionCreator: string | TypedActionCreator<any>,
972955
reducer: CaseReducer<State>,
@@ -1014,8 +997,6 @@ export function buildCreateSlice<
1014997
return context
1015998
},
1016999
getInitialState,
1017-
name,
1018-
reducerPath,
10191000
selectSlice(state) {
10201001
const sliceState = state[reducerPath]
10211002
if (typeof sliceState === 'undefined') {
@@ -1045,6 +1026,7 @@ export function buildCreateSlice<
10451026
const reducerDetails: ReducerDetails = {
10461027
sliceName: name,
10471028
reducerName,
1029+
reducerPath,
10481030
type: getType(name, reducerName),
10491031
}
10501032
handler(
@@ -1060,6 +1042,7 @@ export function buildCreateSlice<
10601042
const reducerDetails: ReducerDetails = {
10611043
sliceName: name,
10621044
reducerName,
1045+
reducerPath,
10631046
type: getType(name, reducerName),
10641047
}
10651048
const handler =

0 commit comments

Comments
 (0)