Incorrect TypeScript declaration
See original GitHub issueTypeScript type declaration created in #27 is incorrect. It doesn’t allow combining of multiple reducers without an initial state.
Both examples below are taken from tests (with some modifications).
const initialState = {A: 0, B: 0};
type State = typeof initialState;
// this one is OK
const reducer1 = reduceReducer(initialState, (state: State, action: Action) => ({
...state,
A: state.A + 1
}));
// this one fails because first argument is expected to be initial state
const reducer2 = reduceReducer(
(state: State, action: Action) => ({...state, A: state.A + 2}),
(state: State, action: Action) => ({...state, B: state.B * 2}),
);
The second example fails with the following error:
Argument of type '(state: { A: number; B: number; }, action: Action) => { B: number; A: number; }' is not assignable to parameter of type 'Reducer<(state: { A: number; B: number; }, action: Action) => { A: number; B: number; }>'.
Types of parameters 'state' and 'state' are incompatible.
Type '(state: { A: number; B: number; }, action: Action) => { A: number; B: number; }' is missing the following properties from type '{ A: number; B: number; }': A, B
Link to TypeScript Playground
https://www.typescriptlang.org/play/index.html#src=%2F%2F type declarations%2C copy from https%3A%2F%2Fgithub.com%2Fredux-utilities%2Freduce-reducers%2Fblob%2Fmaster%2Findex.d.ts type Action %3D { type%3A string%3B }%3B type Reducer<S> %3D (state%3A S%2C action%3A Action) %3D> S%3B declare function reduceReducer<S>( initialState%3A S%2C ...reducers%3A Array<Reducer<S>> )%3A Reducer<S>%3B %2F%2F reduce-reducers use%2C modified examples from https%3A%2F%2Fgithub.com%2Fredux-utilities%2Freduce-reducers%2Fblob%2Fmaster%2Ftest%2Findex.test.js const initialState %3D {A%3A 0%2C B%3A 0}%3B type State %3D typeof initialState%3B %2F%2F this one is OK const reducer1 %3D reduceReducer(initialState%2C (state%3A State%2C action%3A Action) %3D> ({ ...state%2C A%3A state.A %2B 1 }))%3B%0D%0A%0D%0A%2F%2F%20this%20one%20fails%20because%20first%20argument%20is%20expected%20to%20be%20initial%20state%0D%0Aconst%20reducer2%20%3D%20reduceReducer(%0D%0A%20%20(state%3A%20State%2C%20action%3A%20Action)%20%3D%3E%20(%7B…state%2C%20A%3A%20state.A%20%2B%202%7D)%2C%0D%0A%20%20(state%3A%20State%2C%20action%3A%20Action)%20%3D%3E%20(%7B…state%2C%20B%3A%20state.B%20*%202%7D)%2C%0D%0A)%3B%0D%0A%0D%0A%2F%2F%20above%20error%3A%0D%0A%2F%2F%20Argument%20of%20type%20’(state%3A%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%2C%20action%3A%20Action)%20%3D%3E%20%7B%20B%3A%20number%3B%20A%3A%20number%3B%20%7D’%20is%20not%20assignable%20to%20parameter%20of%20type%20’Reducer%3C(state%3A%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%2C%20action%3A%20Action)%20%3D%3E%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%3E’.%0D%0A%2F%2F%20Types%20of%20parameters%20’state’%20and%20’state’%20are%20incompatible.%0D%0A%2F%2F%20Type%20’(state%3A%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D%2C%20action%3A%20Action)%20%3D%3E%20%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D’%20is%20missing%20the%20following%20properties%20from%20type%20’%7B%20A%3A%20number%3B%20B%3A%20number%3B%20%7D’%3A%20A%2C%20B%0D%0A
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
Fixed in
v1.0.2
.Thanks everyone 👏
@vhenzl Right. About point nr1 Did not notice that you could call it without initialState all together. About points 2 and 3 I can’t comment as I have not used or needed to use those.