Skip to content

Previous implementations of Redux reducers using combineReducers broken in 2.4.1 #16795

Closed
@jimsugg

Description

@jimsugg

TypeScript Version: 2.4.1
Code

// From Redux declaration file
interface Action {
  type: any;
}
type Reducer<S> = <A extends Action>(state: S, action: A) => S;
interface ReducersMapObject {
  [key: string]: Reducer<any>;
}
declare function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>;

// User code
interface BsDmBaseAction extends Action {
  type: string;  // override to a string type
  payload: number;
}

interface State1 {
  id: string;
  data: number;
}

function statesById(state: State1[] = [], action: BsDmBaseAction): State1[] {
  switch (action.type) {
    case 'STOP': return [{id: 'status', data: action.payload}];
  }
  return state;
}

function allIds(state: string[] = [], action: BsDmBaseAction): string[] {
  switch (action.type) {
    case 'STOP': return ['status'];
  }
  return state;
}

const bigReducer = combineReducers({
  statesById,
  allIds,
});

Expected behavior:
No errors. This has been working since Typescript 1.8, and works in 2.3.4.

Actual behavior:
Starting with Typescript 2.4.1, the following error is emitted for the combineReducers call:
Error:(40, 36) TS2345:Argument of type '{ statesById: (state: State1[], action: BsDmBaseAction) => State1[]; allIds: (state: string[], ac...' is not assignable to parameter of type 'ReducersMapObject'.
Property 'statesById' is incompatible with index signature.
Type '(state: State1[], action: BsDmBaseAction) => State1[]' is not assignable to type 'Reducer'.
Types of parameters 'action' and 'action' are incompatible.
Type 'A' is not assignable to type 'BsDmBaseAction'.
Type 'Action' is not assignable to type 'BsDmBaseAction'.
Property 'payload' is missing in type 'Action'.

Notes
I do not see any explanation in the various notes on 'breaking changes' to explain this. There are no weak types here, which was involved in the main 'breaking change' description.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions