gpbaculio
gpbaculio

Reputation: 5968

can't assign reducer on typescript?

I am following a microsoft tutorial on redux + typescript here: https://github.com/Microsoft/TypeScript-React-Starter but somehow on the last part I am stuck:

const store = createStore<StoreState, any, any, any>(
  enthusiasm,
  {
    enthusiasmLevel: 1,
    languageName: 'TypeScript'
  },
  composeEnhancers(applyMiddleware(thunk))
);

this is the error I am getting along with the screenshot below:

Argument of type '(state: StoreState, action: EnthusiasmAction) => StoreState' is not assignable to parameter of type 'Reducer<StoreState, any>'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'StoreState | undefined' is not assignable to type 'StoreState'.
      Type 'undefined' is not assignable to type 'StoreState'.ts(2345)

enter image description here

I believe I have followed everyhing and here, I want to add thunk also, but i got error on the argument enthusiasm, and i dont know why, help?

Upvotes: 2

Views: 609

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073978

The key part is here:

Type 'StoreState | undefined' is not assignable to type 'StoreState'.
      Type 'undefined' is not assignable to type 'StoreState'.ts(2345)

It sounds like you need to enable strict null checks (--strictNullChecks). Without them, basically every type in your code is implicitly YourType | undefined, but what you're providing this callback to seems to require a callback that accepts/provides exactly StoreState, not StoreState | undefined. More on nullable types and strict null checks here.

(Or I could have the wrong end of the stick and you do have strict null checks enabled, but what you're using expects them not to be. But I think I have it the right way around.)

Upvotes: 1

Related Questions