Skip to content
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 and union types #29

Closed
OliverJAsh opened this issue Aug 12, 2018 · 1 comment
Closed

Omit and union types #29

OliverJAsh opened this issue Aug 12, 2018 · 1 comment

Comments

@OliverJAsh
Copy link

Currently, if a union type is given to Omit, the returned type does not correctly preserve the union type, e.g. this test fails:

declare const foo: Omit<{ shared: string; } & ({ x: boolean } | { y: string }), 'shared'>;

/* $ExpectType Pick<{
    shared: string;
} & {
    x: boolean;
}, "x"> | Pick<{
    shared: string;
} & {
    y: string;
}, "y"> */
foo;

foo instead has type

Pick<({
    shared: string;
} & {
    x: boolean;
}) | ({
    shared: string;
} & {
    y: string;
}), never>

We can use conditional types to fix this:

export type Omit<T, K extends keyof any> = T extends any ? Pick<T, Exclude<keyof T, K>> : never;

microsoft/TypeScript#12215 (comment)

We might also want to apply this fix to some of the other helpers?

@pelotom
Copy link
Owner

pelotom commented Aug 13, 2018

@OliverJAsh sounds good, want to open a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants