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
// Here's the original reducertypeCounterAction={type: 'INCREMENT'}|{type: 'DECREMENT'};functioncounter(state=0,action: CounterAction){switch(action.type){case'INCREMENT':
returnstate+1;case'DECREMENT':
returnstate-1;default:
returnstate;}}constoriginalStore=createStore(counter);// Here's a new reducertypeStringCaserAction={type: 'UPPER_CASE'}|{type: 'LOWER_CASE'};functionstringCaser(state='testing',action: StringCaserAction){switch(action.type){case'UPPER_CASE':
returnstate.toUpperCase();case'LOWER_CASE':
returnstate.toLowerCase();default:
returnstate;}}constnewStore=originalStore.replaceReducer(stringCaser);// oknewStore.dispatch({type: 'UPPER_CASE'});// I can still use the old store ?!?// But the old store has been mutated and now has the new reducer// The state is incorrectly typed as a number here// It will actually return a string because the reducer has been replacedconstmyNumber=originalStore.getState();
Output
functioncounter(state=0,action){switch(action.type){case'INCREMENT':
returnstate+1;case'DECREMENT':
returnstate-1;default:
returnstate;}}constoriginalStore=createStore(counter);functionstringCaser(state='testing',action){switch(action.type){case'UPPER_CASE':
returnstate.toUpperCase();case'LOWER_CASE':
returnstate.toLowerCase();default:
returnstate;}}constnewStore=originalStore.replaceReducer(stringCaser);// oknewStore.dispatch({type: 'UPPER_CASE'});// I can still use the old store ?!?// But the old store has been mutated and now has the new reducer// The state is incorrectly typed as a number here// It will actually return a string because the reducer has been replacedconstmyNumber=originalStore.getState();
There's no way to express this type of mutation in types. It would be better to remove the return type for replaceReducer. This would then force them to cast the reducer and the store so that they realize they shouldn't be using the original store because it's types are wrong.
Environment Details
N/A
The text was updated successfully, but these errors were encountered:
Methuselah96
changed the title
replaceReducer type is problematic
replaceReducer doesn't account for the mutation to the original store
May 9, 2020
Methuselah96
changed the title
replaceReducer doesn't account for the mutation to the original store
replaceReducer type doesn't account for the mutation to the original store
May 9, 2020
Prior Issues
What is the current behavior?
Code
Output
Compiler Options
Playground Link: Provided
Steps to Reproduce
See code above.
What is the expected behavior?
There's no way to express this type of mutation in types. It would be better to remove the return type for
replaceReducer
. This would then force them to cast the reducer and the store so that they realize they shouldn't be using the original store because it's types are wrong.Environment Details
N/A
The text was updated successfully, but these errors were encountered: