Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for polymorphic action #252

Open
kylegoetz opened this issue Mar 13, 2021 · 0 comments
Open

Documentation for polymorphic action #252

kylegoetz opened this issue Mar 13, 2021 · 0 comments

Comments

@kylegoetz
Copy link

kylegoetz commented Mar 13, 2021

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

interface Foo {
  bar: string
  baz: number
  // 100 other properties
}

I want an action called setFooProperty that will take <K extends keyof Foo>(key:K, value: Foo[K]) => ...

Typing this is no problem. Either

<K extends ...>() => createAction('DESCRIPTION')<{key:K, value:Foo[K]}>()`

or

createAction('DESCRIPTION', action => <K extends ...>(key:K, newVal: Foo[K]) => action({key,value:newVal})

The problem arises when I rely on the type inference:

const actions = { /* some other actions */, setFooProperty }
type Action = ActionType<typeof actions>

const reducer = (state:Foo, action:Action) => {
  switch(action.type) {
    case getType(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 factory k => 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant