Skip to content

actions don't get inferred when the type's fields are all optional #1423

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

Closed
lourenci opened this issue Aug 18, 2021 · 4 comments · Fixed by #3738
Closed

actions don't get inferred when the type's fields are all optional #1423

lourenci opened this issue Aug 18, 2021 · 4 comments · Fixed by #3738
Labels
bug Something isn't working docs TypeScript
Milestone

Comments

@lourenci
Copy link

Hi, thanks for the library.

Probably, some mistake from my side. But, I don't get the action inferred when all the type's fields are optional. Shouldn't toolkit infer the action type by the generic of PayloadAction?

ok:
image

with error:
image

@phryneas
Copy link
Member

Yeah, but at that moment it cannot distinguish it any more from unknown or the empty type (that pretty much equals any).
Could be solvable though. I'll add it to the list.

@phryneas phryneas added the bug Something isn't working label Aug 20, 2021
@lourenci
Copy link
Author

Thank you for your reply, @phryneas. I would love to help on this, but I'm a layman with typescript right know.

Just out of knowing what are the side effects on this (probably, you already know): The match function is not being able to infer the payload action.

image

@phryneas
Copy link
Member

Yupp.

For your case, you could do something like an AtLeastOne type:

interface Filters {
  name: string
  age: number
  city: string
}

type AtLeastOne<T extends Record<string, any>> = keyof T extends infer K ? K extends string ? Pick<T, K & keyof T> & Partial<T> : never : never

type AtLeastOneFilter = AtLeastOne<Filters>

@markerikson
Copy link
Collaborator

For now we should probably add a docs entry in the "TS Usage" page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docs TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants