Skip to content

Redux Reducer type error #17154

Closed
Closed
@zhahaoyu

Description

@zhahaoyu

I am trying to define reducers used in redux. I don't know why this definition not works.

TypeScript Version: 2.4.1

Code

// Type definitions from 'redux'
export interface Action {
  type: any;
}

export type Reducer<S> = <A extends Action>(state: S, action: A) => S;
// My code
import { Action, Reducer } from 'redux';

enum Types {
  add,
}

interface Increment extends Action {
  type: Types.add;
  value: number;
}

const count: Reducer<number> = (state: number, action: Increment) => {
  if (action.type === Types.add) {
    return state + action.value;
  }
  return state;
};

Expected behavior:
This should work!

Actual behavior:

(14,7): error TS2322: Type '(state: number, action: Increment) => number' is not assignable to type 'Reducer<number>'.
  Types of parameters 'action' and 'action' are incompatible.
    Type 'A' is not assignable to type 'Increment'.
      Type 'Action' is not assignable to type 'Increment'.
        Property 'value' is missing in type 'Action'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions