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.
Problem
For example, I have next code:
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.