dphilipson / typescript-fsa-reducers

Fluent syntax for defining typesafe reducers on top of typescript-fsa.
MIT License
220 stars 16 forks source link

Reducer without initial state does not assept undefined by default #36

Closed sgrishchenko closed 4 years ago

sgrishchenko commented 4 years ago

Problem

For example, I have next code:

import { actionCreatorFactory } from "typescript-fsa";
import { reducerWithoutInitialState } from "typescript-fsa-reducers";

const actionCreator = actionCreatorFactory("Counter");

const increment = actionCreator("INCREMENT");
const decrement = actionCreator("DECREMENT");

type State = {
  count: number;
};

const reducer = reducerWithoutInitialState<State>()
  .case(increment, state => ({
    ...state,
    count: state.count + 1
  }))
  .case(decrement, state => ({
    ...state,
    count: state.count - 1
  }));

reducer(undefined, decrement()); // <- runtime error will be here

As you can see, I can put undefined in reducer without initial state but in case handler state look like defined and I don't receive any Typescript error. You cat test this code here https://codesandbox.io/s/cranky-wildflower-z0xky

Solution

Avoid to pass undefined to reducer without initial state. I think it will be more more honest.

dphilipson commented 4 years ago

Published in v1.2.2.