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

New definition for omit that should ensure the name Omit is preserved… #37608

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,9 @@ type Extract<T, U> = T extends U ? T : never;
/**
* Construct a type with the properties of T except for those in type K.
*/
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
type Omit<T, K extends keyof any, Keys extends keyof T = Exclude<keyof T, K>> = {
[P in Keys]: T[P];
};
weswigham marked this conversation as resolved.
Show resolved Hide resolved

/**
* Exclude null and undefined from T
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/genericIsNeverEmptyObject.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// Repro from #29067

function test<T extends { a: string }>(obj: T) {
>test : <T extends { a: string; }>(obj: T) => Pick<T, Exclude<keyof T, "a">> & { b: string; }
>test : <T extends { a: string; }>(obj: T) => Omit<T, "a", Exclude<keyof T, "a">> & { b: string; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the self-generating third argument is at least as bad as converting Omit to its definition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth investing in hiding that argument when it matches the default? (As it always should)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it solves one problem -- reading the type -- but not the second problem: writing the type.

And if it applies everywhere, the mismatch might just be confusing. Actually, hiding any optional item entirely is likely to be confusing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I’d prefer #31205 (comment):

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

>a : string
>obj : T

let { a, ...rest } = obj;
>a : string
>rest : Pick<T, Exclude<keyof T, "a">>
>rest : Omit<T, "a", Exclude<keyof T, "a">>
>obj : T

return { ...rest, b: a };
>{ ...rest, b: a } : Pick<T, Exclude<keyof T, "a">> & { b: string; }
>rest : Pick<T, Exclude<keyof T, "a">>
>{ ...rest, b: a } : Omit<T, "a", Exclude<keyof T, "a">> & { b: string; }
>rest : Omit<T, "a", Exclude<keyof T, "a">>
>b : string
>a : string
}
Expand All @@ -30,7 +30,7 @@ let o2: { b: string, x: number } = test(o1);
>o2 : { b: string; x: number; }
>b : string
>x : number
>test(o1) : Pick<{ a: string; x: number; }, "x"> & { b: string; }
>test : <T extends { a: string; }>(obj: T) => Pick<T, Exclude<keyof T, "a">> & { b: string; }
>test(o1) : Omit<{ a: string; x: number; }, "a", "x"> & { b: string; }
>test : <T extends { a: string; }>(obj: T) => Omit<T, "a", Exclude<keyof T, "a">> & { b: string; }
>o1 : { a: string; x: number; }

16 changes: 8 additions & 8 deletions tests/baselines/reference/genericObjectRest.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@ function f1<T extends { a: string, b: number }>(obj: T) {
let { a: a1, ...r1 } = obj;
>a : any
>a1 : string
>r1 : Pick<T, Exclude<keyof T, "a">>
>r1 : Omit<T, "a", Exclude<keyof T, "a">>
>obj : T

let { a: a2, b: b2, ...r2 } = obj;
>a : any
>a2 : string
>b : any
>b2 : number
>r2 : Pick<T, Exclude<keyof T, "a" | "b">>
>r2 : Omit<T, "a" | "b", Exclude<keyof T, "a" | "b">>
>obj : T

let { 'a': a3, ...r3 } = obj;
>a3 : string
>r3 : Pick<T, Exclude<keyof T, "a">>
>r3 : Omit<T, "a", Exclude<keyof T, "a">>
>obj : T

let { ['a']: a4, ...r4 } = obj;
>'a' : "a"
>a4 : string
>r4 : Pick<T, Exclude<keyof T, "a">>
>r4 : Omit<T, "a", Exclude<keyof T, "a">>
>obj : T

let { [a]: a5, ...r5 } = obj;
>a : "a"
>a5 : string
>r5 : Pick<T, Exclude<keyof T, "a">>
>r5 : Omit<T, "a", Exclude<keyof T, "a">>
>obj : T
}

Expand All @@ -68,7 +68,7 @@ function f2<T extends { [sa]: string, [sb]: number }>(obj: T) {
>a1 : string
>sb : unique symbol
>b1 : number
>r1 : Pick<T, Exclude<keyof T, unique symbol | unique symbol>>
>r1 : Omit<T, unique symbol | unique symbol, Exclude<keyof T, unique symbol | unique symbol>>
>obj : T
}

Expand All @@ -83,7 +83,7 @@ function f3<T, K1 extends keyof T, K2 extends keyof T>(obj: T, k1: K1, k2: K2) {
>a1 : T[K1]
>k2 : K2
>a2 : T[K2]
>r1 : Pick<T, Exclude<keyof T, K1 | K2>>
>r1 : Omit<T, K1 | K2, Exclude<keyof T, K1 | K2>>
>obj : T
}

Expand All @@ -104,7 +104,7 @@ function f4<K1 extends keyof Item, K2 extends keyof Item>(obj: Item, k1: K1, k2:
>a1 : Item[K1]
>k2 : K2
>a2 : Item[K2]
>r1 : Pick<Item, Exclude<"a", K1 | K2> | Exclude<"b", K1 | K2> | Exclude<"c", K1 | K2>>
>r1 : Omit<Item, K1 | K2, Exclude<"a", K1 | K2> | Exclude<"b", K1 | K2> | Exclude<"c", K1 | K2>>
>obj : Item
}

6 changes: 3 additions & 3 deletions tests/baselines/reference/literalTypeWidening.types
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,15 @@ function test<T extends { a: string, b: string }>(obj: T): T {

let { a, ...rest } = obj;
>a : string
>rest : Pick<T, Exclude<keyof T, "a">>
>rest : Omit<T, "a", Exclude<keyof T, "a">>
>obj : T

return { a: 'hello', ...rest } as T;
>{ a: 'hello', ...rest } as T : T
>{ a: 'hello', ...rest } : { a: string; } & Pick<T, Exclude<keyof T, "a">>
>{ a: 'hello', ...rest } : { a: string; } & Omit<T, "a", Exclude<keyof T, "a">>
>a : string
>'hello' : "hello"
>rest : Pick<T, Exclude<keyof T, "a">>
>rest : Omit<T, "a", Exclude<keyof T, "a">>
}

// Repro from #32169
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/mappedTypeConstraints.types
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ const modifier = <T extends TargetProps>(targetProps: T) => {

let {bar, ...rest} = targetProps;
>bar : string
>rest : Pick<T, Exclude<keyof T, "bar">>
>rest : Omit<T, "bar", Exclude<keyof T, "bar">>
>targetProps : T

rest.foo;
>rest.foo : T["foo"]
>rest : Pick<T, Exclude<keyof T, "bar">>
>rest : Omit<T, "bar", Exclude<keyof T, "bar">>
>foo : T["foo"]

};
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/objectRestNegative.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ function stillMustBeLast({ ...mustBeLast, a }: { a: number, b: string }): void {
>b : string
}
function generic<T extends { x, y }>(t: T) {
>generic : <T extends { x: any; y: any; }>(t: T) => Pick<T, Exclude<keyof T, "x">>
>generic : <T extends { x: any; y: any; }>(t: T) => Omit<T, "x", Exclude<keyof T, "x">>
>x : any
>y : any
>t : T

let { x, ...rest } = t;
>x : any
>rest : Pick<T, Exclude<keyof T, "x">>
>rest : Omit<T, "x", Exclude<keyof T, "x">>
>t : T

return rest;
>rest : Pick<T, Exclude<keyof T, "x">>
>rest : Omit<T, "x", Exclude<keyof T, "x">>
}

let rest: { b: string }
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/omitTypeHelperModifiers01.types
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,55 @@ type A = {
};

type B = Omit<A, 'a'>;
>B : Pick<A, "b" | "c" | "d">
>B : Omit<A, "a", "b" | "c" | "d">

function f(x: B) {
>f : (x: Pick<A, "b" | "c" | "d">) => void
>x : Pick<A, "b" | "c" | "d">
>f : (x: Omit<A, "a", "b" | "c" | "d">) => void
>x : Omit<A, "a", "b" | "c" | "d">

const b = x.b;
>b : string | undefined
>x.b : string | undefined
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>b : string | undefined

x.b = "hello";
>x.b = "hello" : "hello"
>x.b : string | undefined
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>b : string | undefined
>"hello" : "hello"

x.b = undefined;
>x.b = undefined : undefined
>x.b : string | undefined
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>b : string | undefined
>undefined : undefined

const c = x.c;
>c : boolean
>x.c : boolean
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>c : boolean

x.c = true;
>x.c = true : true
>x.c : any
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>c : any
>true : true

const d = x.d;
>d : unknown
>x.d : unknown
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>d : unknown

x.d = d;
>x.d = d : unknown
>x.d : unknown
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a", "b" | "c" | "d">
>d : unknown
>d : unknown
}
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/omitTypeTestErrors01.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/omitTypeTestErrors01.ts(11,16): error TS2339: Property 'c' does not exist on type 'Pick<Foo, "a" | "b">'.
tests/cases/compiler/omitTypeTestErrors01.ts(15,16): error TS2339: Property 'b' does not exist on type 'Pick<Foo, "a">'.
tests/cases/compiler/omitTypeTestErrors01.ts(11,16): error TS2339: Property 'c' does not exist on type 'Omit<Foo, "c", "a" | "b">'.
tests/cases/compiler/omitTypeTestErrors01.ts(15,16): error TS2339: Property 'b' does not exist on type 'Omit<Foo, "c" | "b", "a">'.


==== tests/cases/compiler/omitTypeTestErrors01.ts (2 errors) ====
Expand All @@ -15,13 +15,13 @@ tests/cases/compiler/omitTypeTestErrors01.ts(15,16): error TS2339: Property 'b'
export function getBarC(bar: Bar) {
return bar.c;
~
!!! error TS2339: Property 'c' does not exist on type 'Pick<Foo, "a" | "b">'.
!!! error TS2339: Property 'c' does not exist on type 'Omit<Foo, "c", "a" | "b">'.
}

export function getBazB(baz: Baz) {
return baz.b;
~
!!! error TS2339: Property 'b' does not exist on type 'Pick<Foo, "a">'.
!!! error TS2339: Property 'b' does not exist on type 'Omit<Foo, "c" | "b", "a">'.
}


16 changes: 8 additions & 8 deletions tests/baselines/reference/omitTypeTestErrors01.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ interface Foo {
}

export type Bar = Omit<Foo, "c">;
>Bar : Pick<Foo, "a" | "b">
>Bar : Omit<Foo, "c", "a" | "b">

export type Baz = Omit<Foo, "b" | "c">;
>Baz : Pick<Foo, "a">
>Baz : Omit<Foo, "c" | "b", "a">

export function getBarC(bar: Bar) {
>getBarC : (bar: Pick<Foo, "a" | "b">) => any
>bar : Pick<Foo, "a" | "b">
>getBarC : (bar: Omit<Foo, "c", "a" | "b">) => any
>bar : Omit<Foo, "c", "a" | "b">

return bar.c;
>bar.c : any
>bar : Pick<Foo, "a" | "b">
>bar : Omit<Foo, "c", "a" | "b">
>c : any
}

export function getBazB(baz: Baz) {
>getBazB : (baz: Pick<Foo, "a">) => any
>baz : Pick<Foo, "a">
>getBazB : (baz: Omit<Foo, "c" | "b", "a">) => any
>baz : Omit<Foo, "c" | "b", "a">

return baz.b;
>baz.b : any
>baz : Pick<Foo, "a">
>baz : Omit<Foo, "c" | "b", "a">
>b : any
}

Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/omitTypeTests01.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ interface Foo {
}

export type Bar = Omit<Foo, "c">;
>Bar : Pick<Foo, "a" | "b">
>Bar : Omit<Foo, "c", "a" | "b">

export type Baz = Omit<Foo, "b" | "c">;
>Baz : Pick<Foo, "a">
>Baz : Omit<Foo, "c" | "b", "a">

export function getBarA(bar: Bar) {
>getBarA : (bar: Pick<Foo, "a" | "b">) => string
>bar : Pick<Foo, "a" | "b">
>getBarA : (bar: Omit<Foo, "c", "a" | "b">) => string
>bar : Omit<Foo, "c", "a" | "b">

return bar.a;
>bar.a : string
>bar : Pick<Foo, "a" | "b">
>bar : Omit<Foo, "c", "a" | "b">
>a : string
}

export function getBazA(baz: Baz) {
>getBazA : (baz: Pick<Foo, "a">) => string
>baz : Pick<Foo, "a">
>getBazA : (baz: Omit<Foo, "c" | "b", "a">) => string
>baz : Omit<Foo, "c" | "b", "a">

return baz.a;
>baz.a : string
>baz : Pick<Foo, "a">
>baz : Omit<Foo, "c" | "b", "a">
>a : string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export interface RouteComponentProps { route: string }
>route : string

declare function withRouter<
>withRouter : <P extends RouteComponentProps, C extends ComponentType<P>>(component: (C & FunctionComponent<P>) | (C & ComponentClass<P>)) => ComponentClass<Pick<P, Exclude<keyof P, "route">>>
>withRouter : <P extends RouteComponentProps, C extends ComponentType<P>>(component: (C & FunctionComponent<P>) | (C & ComponentClass<P>)) => ComponentClass<Omit<P, "route", Exclude<keyof P, "route">>>

P extends RouteComponentProps,
C extends ComponentType<P>
Expand All @@ -174,8 +174,8 @@ declare const MyComponent: ComponentType<Props>;
>MyComponent : ComponentType<Props>

withRouter(MyComponent);
>withRouter(MyComponent) : ComponentClass<Pick<Props, "username">>
>withRouter : <P extends RouteComponentProps, C extends ComponentType<P>>(component: (C & FunctionComponent<P>) | (C & ComponentClass<P>)) => ComponentClass<Pick<P, Exclude<keyof P, "route">>>
>withRouter(MyComponent) : ComponentClass<Omit<Props, "route", "username">>
>withRouter : <P extends RouteComponentProps, C extends ComponentType<P>>(component: (C & FunctionComponent<P>) | (C & ComponentClass<P>)) => ComponentClass<Omit<P, "route", Exclude<keyof P, "route">>>
>MyComponent : ComponentType<Props>

// Repro from #33490
Expand Down