Skip to content
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

Redux Reducer type error #17154

Closed
zhahaoyu opened this issue Jul 13, 2017 · 4 comments
Closed

Redux Reducer type error #17154

zhahaoyu opened this issue Jul 13, 2017 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@zhahaoyu
Copy link

zhahaoyu commented Jul 13, 2017

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'.
@zhahaoyu
Copy link
Author

This works in Typescript 2.3.4

@kitsonk
Copy link
Contributor

kitsonk commented Jul 13, 2017

Dupe of #16985

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 28, 2017

Not exactly a dupe of that. This is due to stricter generic checks though, which that issue is related to.

You're trying to assign a (state: number, action: Increment) => number to a <A extends Action>(state: number, action: A) => number, meaning that you're expecting more than you may potentially be given. So your Reducer definition is incorrect.

Check out reduxjs/redux#2467 for the fix I put out to Redux which includes an explanation.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 28, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 12, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Sep 12, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants