-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Omit
behaves poorly with unions
#39556
Comments
Duplicate of #31501 search terms: It's working as intended. You need to use conditional types to process each union member separately |
Hm, ok. Thanks for the workaround. Is it perhaps worth updating the documentation to explain that |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This behaviour is quite unexpected and made me lose many hours before realising it didn't work as I thought. I hope this helps future students. Original code for `UnionOmit` by [Andrii Dieiev](https://github.com/IllusionMH), from: microsoft/TypeScript#39556 (comment)
Uhm, how is this "working as intended"? |
The See the following for example of distribution: type ToArrayDistributive<T> = T extends any ? T[] : never;
type ToArrayNonDistributive<T> = T[];
type ExampleDistributive = ToArrayDistributive<string | number>; // string[] | number[]
type ExampleNonDistributive = ToArrayNonDistributive<string | number>; // (string | number)[] To distribute the type parameter of type OmitDistributive<T, K extends keyof any> = T extends any ? Omit<T, K> : never; |
See microsoft/TypeScript#39556 and many other related issues. Fixes Automattic#13529.
See microsoft/TypeScript#39556 and many other related issues. Fixes Automattic#13529.
TypeScript Version: 3.9.2 (also v4.0.0-beta)
Search Terms: omit, pick, union
Code
Expected behavior:
No type error on the
let a
, type error on thelet b
.Actual behavior:
Type error on the
let a
:No type error on the
let b
.Specifically, it is surprising to me that, contrary to its description, the result of
Omit<T, K>
is not the type which contains the members ofT
except without the properties inK
, but instead some other type.Playground Link: Link
Related Issues: None that I could find.
The text was updated successfully, but these errors were encountered: