cartant/ts-action

reducer() is not compatible with Redux reducer spec

Closed this issue · 2 comments

osak commented

Following code does not compile with --strict option:

import {action, reducer, on} from 'ts-action';
import {combineReducers, createStore} from 'redux';

const A = action('a');

interface State {}

const rootReducer = reducer<State>([
    on(A, (s, a) => s)
], {});

createStore(rootReducer);

with error:

% npx tsc --version
Version 2.9.2
% npx tsc --strict a.ts
a.ts:12:13 - error TS2345: Argument of type 'Reducer<State>' is not assignable to parameter of type 'Reducer<State, Action<string>>'.
  Types of parameters 'state' and 'state' are incompatible.
    Type 'State | undefined' is not assignable to type 'State'.
      Type 'undefined' is not assignable to type 'State'.

12 createStore(rootReducer);
               ~~~~~~~~~~~

Apparently Redux's Reducer is designed to accept undefined as state, as explained in the official document, but ts-action's Reducer does not.

Yep, the state parameter in the Reducer type should support undefined being passed:

export type Reducer<S> = (state: S | undefined, action: Action<string>) => S;
                                 ~~~~~~~~~~~~~

Well spotted. Thanks.

Should be fixed in 6.0.3.