You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the state is a number, string or boolean, and the reducer function uses no parameters (() => newState), the generic type of the action reducer is wrongly inferred to be Number, String or Boolean.
This leads to Typescript errors either in createReducer() itself (depending on the order or the action reducers) or later on.
This was not an issue in @ngrx/store v10 - I hit this as I've upgraded to v11.
Minimal reproduction of the bug/regression with instructions:
import{Action,ActionReducer,createAction,createReducer,on,props}from'@ngrx/store';constinitialState: number=0;constsetAction=createAction('set',props<{value: number}>());constresetAction=createAction('reset');exportconstfailingReducer=createReducer(initialState,on(setAction,(_state,{ value })=>value),// Typescript erroron(resetAction,()=>initialState),);exportconstworkingReducer=createReducer(initialState,on(resetAction,()=>initialState),on(setAction,(_state,{ value })=>value),);exportconstexpectedActionReducer: ActionReducer<number,Action>=workingReducer;// Typescript errorexportconstactualActionReducer: ActionReducer<Number,Action>=workingReducer;
Expected behavior:
If the state is number, string, or boolean, reducers' inferred generic type should be the same, even if there is a "reset-style" action reducer.
Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):
NgRx: 11.0.1
Typescript: 4.1.5 / 4.2.3
Other information:
The workaround is to add the state argument to the reducer function even though it is not needed:
exportconstreducer=createReducer(initialState,on(setAction,(_state,{ value })=>value),on(resetAction,(_state)=>initialState),);// works!exportconstexpectedActionReducer: ActionReducer<number,Action>=reducer;
I would be willing to submit a PR to fix this issue
[ ] Yes (Assistance is provided if you need help submitting a pull request)
[X] No
The text was updated successfully, but these errors were encountered:
lephyrus
changed the title
Ordering of on() reducers in createReducer() params can lead to type error
Generic type of reducers Number/String/Boolean instead of number/string/booleanMar 10, 2021
lephyrus
changed the title
Generic type of reducers Number/String/Boolean instead of number/string/boolean
Inferred generic type of reducers Number/String/Boolean instead of number/string/boolean
Mar 10, 2021
When the state is a
number
,string
orboolean
, and the reducer function uses no parameters (() => newState
), the generic type of the action reducer is wrongly inferred to beNumber
,String
orBoolean
.This leads to Typescript errors either in
createReducer()
itself (depending on the order or the action reducers) or later on.This was not an issue in
@ngrx/store
v10 - I hit this as I've upgraded to v11.Minimal reproduction of the bug/regression with instructions:
Reproduction in the Typescript playground.
Expected behavior:
If the state is
number
,string
, orboolean
, reducers' inferred generic type should be the same, even if there is a "reset-style" action reducer.Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):
NgRx: 11.0.1
Typescript: 4.1.5 / 4.2.3
Other information:
The workaround is to add the state argument to the reducer function even though it is not needed:
I would be willing to submit a PR to fix this issue
[ ] Yes (Assistance is provided if you need help submitting a pull request)
[X] No
The text was updated successfully, but these errors were encountered: