Skip to content

Omit<A | B> works unexpectly, cannot keep union relationship #41772

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
k8w opened this issue Dec 2, 2020 · 3 comments
Closed

Omit<A | B> works unexpectly, cannot keep union relationship #41772

k8w opened this issue Dec 2, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@k8w
Copy link

k8w commented Dec 2, 2020

TypeScript Version: 3.7.x-dev.201xxxxx
4.0.3

Search Terms:
Omit Union Omit<A|B>

Code

type A = {
    type: 'A',
    valueA: string,
    common: string
}
type B = {
    type: 'B',
    valueB: string,
    common: string
}
type AB = A | B;
type NoCommon = Omit<AB, 'common'>;

let value: NoCommon = {
    type: 'A',
    valueA: 'XXX'
}

Expected behavior:
No compile error

Actual behavior:

error TS2322: Type '{ type: "A"; valueA: string; }' is not assignable to type 'Pick<AB, "type">'.
Object literal may only specify known properties, and 'valueA' does not exist in type 'Pick<AB, "type">'.
16 valueA: 'XXX'

Playground Link:

Related Issues:

@k8w k8w changed the title Omit<A | B> works unexpectly Omit<A | B> works unexpectly, cannot keep union relationship Dec 2, 2020
@MartinJohns
Copy link
Contributor

Duplicate of #39556. Search terms: omit union

Search Terms: Omit Union Omit<A|B>

A tip for the future: Something overly specific to your example, such as the types A and B, don't make good search terms. :-)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 3, 2020
@RyanCavanaugh
Copy link
Member

To get the behavior you wanted, the syntax is

type NoCommonHelper<T> = T extends unknown ? Omit<T, 'common'> : never;
type NoCommon = NoCommonHelper<AB>;

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants