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
Given the problem @phryneas states in his article, unions are definitely prone to errors.
I think it could be valuable to ship a type helper OtherActions<T> that has the purpose of creating a complement set of actions for a given reducer.
The implementation of OtherActions would look like this:
typeOtherActions<TextendsAnyAction>={type: TextendsT['type'] ? T : {type: unknown}}
And its usage would look like this:
typeActions=SendMessageAction|DeleteMessageAction;exportfunctionchatReducer(state=initialState,action: Actions|OtherActions<Actions>): ChatState{switch(action.type){caseSEND_MESSAGE:
return{messages: [...state.messages,action.messages]}caseDELETE_MESSAGE:
return{messages: state.messages// etc etc}// NOTE: notice how it errors if this is commented out// default:// return state}}
This change here would make it so the action param includes the complement of any union given to it. This would force typescript users to return a default case.
I don't think this is actually going to solve any problems. Instead of having them remember to add a default case to their reducer, they now have to remember to use this extra type. That still puts the onus on the user and just shifts where they have to remember these things. So, it's a net zero gain.
Nonetheless, we already have an open issue for this.
New Features
What is the new or updated feature that you are suggesting?
OtherActions<T>
type. (see #4025 (comment) for context)Given the problem @phryneas states in his article, unions are definitely prone to errors.
I think it could be valuable to ship a type helper
OtherActions<T>
that has the purpose of creating a complement set of actions for a given reducer.The implementation of
OtherActions
would look like this:And its usage would look like this:
TS Playground Link
Additionally, I think it could be beneficial to use OtherActions in the definition of
Reducer<T>
Why should this feature be included?
This change here would make it so the action param includes the complement of any union given to it. This would force typescript users to return a default case.
TS Playground Link
What docs changes are needed to explain this?
This would be included in #4025
The text was updated successfully, but these errors were encountered: