-
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
Omitting a property on a discriminated union type #54525
Comments
This is unfortunately working as intended. Omit does not distribute over the union. There are many implementations of a distributive omit out there, just search for it using "distributive omit". See also #54451. |
You can create your own Omit with Distributive Conditional Types:
|
@nurulakbaral Be aware that this version will not warn you if you mistype the property name, while the version suggested in #54451 will error, but only allows to omit properties common to all types of the union. |
type TheOmit<T, K extends keyof T> = T extends any ? Omit<T, K> : never How about this? |
Duplicate of #31501 |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
1 similar comment
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Bug Report
When using Omit on a discriminated union type, the properties unique to each "branch" of the union type are lost.
🔎 Search Terms
Omit, union, discriminated union
🕗 Version & Regression Information
Tested on TypeScript 4.8.4 and on the nightly version (June 5th, 2023)
⏯ Playground Link
demo
Alternative demo (a different way to do discriminated unions, which is also affected)
💻 Code
🙁 Actual behavior
T2 is interpreted as such by TypeScript:
And the following object is seen as invalid:
🙂 Expected behavior
T2 should be interpreted like this instead:
And the object described above should be seen as valid.
The text was updated successfully, but these errors were encountered: