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
Because Pick isn't distributive over union types, when the input type is a union it is effectively treated as a single object type with only the common properties, each having a union of the respective property types. So, Pick sees C as equivalent to { name?: string, variant: 'a' | 'b' } and X therefore is { variant: 'a' | 'b' }, which isn't assignable back to C.
There are two ways you can fix the issue. The simplest is to declare C as a single object type (and this would be my recommendation):
typeC={name?: string,variant: 'a'|'b'};
The other is to make Omit be distributive over union types:
This may look a bit odd, but the T extends T ? XXX : never pattern simply means "when T is a union type, distribute XXX over each constituent of T and union together the results".
When you use the distributive form of Omit you now get
See microsoft/TypeScript#28791 (comment):
The solution I came up with is:
Note that here I want a
Pick
that is distributive, and the above was aboutOmit
. I suppose we'd want both?Not sure what to name this though.
Upvote & Fund
The text was updated successfully, but these errors were encountered: