Since the type of of a reducer is defined as
export type Reducer<T> = (state: T | undefined) => T | undefined;
In my reducers that are not the initial reducer, I must declare
state = state as State
Am I missing something, or is this considered a minor inconvenience, please?
Non-initial reducers can be typed (in TypeScript) as
(state: T) => Tand these will be compatible with theReducer<T>type found in the library. Here is an example from my codebase, the first snippet is an initial reducer that needs to treat the case for undefined:This second snippet is a non-initial reducer where I am sure the previous state is not undefined:
Notice that when these streams are merged, the resulting type can be
Stream<Reducer<State>>with no casting involved: