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
I suggest there be documentation for how to handle a polymorphic action (like one action that could modify N properties of a state with varying property types instead of having N actions, each for changing one property)
suppose I have state
interfaceFoo{bar: stringbaz: number// 100 other properties}
I want an action called setFooProperty that will take <K extends keyof Foo>(key:K, value: Foo[K]) => ...
The problem arises when I rely on the type inference:
constactions={/* some other actions */, setFooProperty }typeAction=ActionType<typeofactions>constreducer=(state:Foo,action:Action)=>{switch(action.type){casegetType(setFooProperty):
// ...}}
With the latter, type inference error
Property 'type' is missing in type '(action: any) => PayloadAction<DESCRIPTION, <K extends ...>(key: K, newVal: Foo[K]) => any>' but required in type 'Action<string>'
With the former, of course I get a type error that an action factoryk => createAction(...) does not match the required type payload => PayloadAction
Assuming there is a way to accomplish this, I suggest it be in the documentation. If it's not possible and instead I need to write N different actions for modifying each property of Foo, then so be it. Just trying to simplify code when it's a simple update.
The text was updated successfully, but these errors were encountered:
I suggest there be documentation for how to handle a polymorphic action (like one action that could modify N properties of a state with varying property types instead of having N actions, each for changing one property)
suppose I have state
I want an action called
setFooProperty
that will take<K extends keyof Foo>(key:K, value: Foo[K]) => ...
Typing this is no problem. Either
or
The problem arises when I rely on the type inference:
With the latter, type inference error
With the former, of course I get a type error that an action factory
k => createAction(...)
does not match the required typepayload => PayloadAction
Assuming there is a way to accomplish this, I suggest it be in the documentation. If it's not possible and instead I need to write N different actions for modifying each property of
Foo
, then so be it. Just trying to simplify code when it's a simple update.The text was updated successfully, but these errors were encountered: