Skip to content

Type issue with union arguments for slice actions #142

@pdobrev

Description

@pdobrev

We've been having issues upgrading TypeScript to 3.4 in a project that uses redux-starter-kit, so I gave v0.5.1 a try and noticed that the following code does not compile anymore:

import { createSlice, PayloadAction, configureStore } from 'redux-starter-kit';

type SceneID = 'main' | 'profile' | 'prefs';

const slice = createSlice({
  initialState: {
    scene: 'main' as SceneID,
  },
  reducers: {
    selectScene: (state, { payload }: PayloadAction<SceneID>) => {
      state.scene = payload;
    },
  },
});

const store = configureStore({
  reducer: slice.reducer,
});

store.dispatch(slice.actions.selectScene('profile'));

The error tsc gives is:

error TS2345: Argument of type '"profile"' is not assignable to parameter of type '"main" & "profile" & "prefs"'.
Type '"profile"' is not assignable to type '"main"'.

It seems that the argument type for the action was converted from a union to an intersection type.

The same code works with redux-starter-kit 0.4.3, but that one has other typing issues that prevent us from upgrading to TypeScript 3.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions