Skip to content

strongly type slice name #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ export { createSelector }
export function createSerializableStateInvariantMiddleware(options?: SerializableStateInvariantMiddlewareOptions): Middleware;

// @public
export function createSlice<State, CaseReducers extends SliceCaseReducers<State>>(options: CreateSliceOptions<State, CaseReducers>): Slice<State, CaseReducers>;
export function createSlice<State, CaseReducers extends SliceCaseReducers<State>, Name extends string = string>(options: CreateSliceOptions<State, CaseReducers, Name>): Slice<State, CaseReducers, Name>;

// @public
export interface CreateSliceOptions<State = any, CR extends SliceCaseReducers<State> = SliceCaseReducers<State>> {
export interface CreateSliceOptions<State = any, CR extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> {
extraReducers?: CaseReducers<NoInfer<State>, any> | ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void);
initialState: State;
name: string;
name: Name;
reducers: ValidateSliceCaseReducers<State, CR>;
}

Expand Down Expand Up @@ -182,10 +182,10 @@ export interface SerializableStateInvariantMiddlewareOptions {
}

// @public
export interface Slice<State = any, CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>> {
export interface Slice<State = any, CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> {
actions: CaseReducerActions<CaseReducers>;
caseReducers: SliceDefinedCaseReducers<CaseReducers>;
name: string;
name: Name;
reducer: Reducer<State>;
}

Expand Down
21 changes: 12 additions & 9 deletions src/createSlice.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Reducer } from 'redux'
import {
ActionCreatorWithoutPayload,
createAction,
PayloadAction,
PayloadActionCreator,
PrepareAction,
ActionCreatorWithoutPayload,
_ActionCreatorWithPreparedPayload
} from './createAction'
import { createReducer, CaseReducers, CaseReducer } from './createReducer'
import { CaseReducer, CaseReducers, createReducer } from './createReducer'
import {
ActionReducerMapBuilder,
executeReducerBuilderCallback
Expand All @@ -29,12 +29,13 @@ export type SliceActionCreator<P> = PayloadActionCreator<P>
*/
export interface Slice<
State = any,
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
Name extends string = string
> {
/**
* The slice name.
*/
name: string
name: Name

/**
* The slice's reducer.
Expand All @@ -61,12 +62,13 @@ export interface Slice<
*/
export interface CreateSliceOptions<
State = any,
CR extends SliceCaseReducers<State> = SliceCaseReducers<State>
CR extends SliceCaseReducers<State> = SliceCaseReducers<State>,
Name extends string = string
> {
/**
* The slice's name. Used to namespace the generated action types.
*/
name: string
name: Name

/**
* The initial state to be returned by the slice reducer.
Expand Down Expand Up @@ -212,10 +214,11 @@ function getType(slice: string, actionKey: string): string {
*/
export function createSlice<
State,
CaseReducers extends SliceCaseReducers<State>
CaseReducers extends SliceCaseReducers<State>,
Name extends string = string
>(
options: CreateSliceOptions<State, CaseReducers>
): Slice<State, CaseReducers> {
options: CreateSliceOptions<State, CaseReducers, Name>
): Slice<State, CaseReducers, Name> {
const { name, initialState } = options
if (!name) {
throw new Error('`name` is a required option for createSlice')
Expand Down
47 changes: 40 additions & 7 deletions type-tests/files/createSlice.typetest.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
import { AnyAction, Reducer, Action } from 'redux'
import { Action, AnyAction, Reducer } from 'redux'
import { ValidateSliceCaseReducers } from 'src/createSlice'
import {
createSlice,
PayloadAction,
createAction,
ActionReducerMapBuilder,
ActionCreatorWithOptionalPayload,
ActionCreatorWithNonInferrablePayload,
ActionCreatorWithOptionalPayload,
ActionCreatorWithoutPayload,
ActionCreatorWithPayload,
ActionCreatorWithPreparedPayload,
ActionReducerMapBuilder,
createAction,
createSlice,
PayloadAction,
SliceCaseReducers
} from '../../src'
import { ValidateSliceCaseReducers } from 'src/createSlice'

function expectType<T>(t: T) {
return t
}

/*
* Test: Slice name is strongly typed.
*/

const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: (state: number, action) => state + action.payload,
decrement: (state: number, action) => state - action.payload
}
})

const uiSlice = createSlice({
name: 'ui',
initialState: 0,
reducers: {
goToNext: (state: number, action) => state + action.payload,
goToPrevious: (state: number, action) => state - action.payload
}
})

const actionCreators = {
[counterSlice.name]: { ...counterSlice.actions },
[uiSlice.name]: { ...uiSlice.actions }
}

expectType<typeof counterSlice.actions>(actionCreators.counter)
expectType<typeof uiSlice.actions>(actionCreators.ui)

// typings:expect-error
const value = actionCreators.anyKey

/*
* Test: createSlice() infers the returned slice's type.
*/
Expand Down