Skip to content

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

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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17402,7 +17402,7 @@ namespace ts {
}
}
}
else if (isGenericMappedType(target) && !target.declaration.nameType) {
else if (isGenericMappedType(target) && (!target.declaration.nameType || relation === comparableRelation)) {
// A source type T is related to a target type { [P in X]: T[P] }
const template = getTemplateTypeFromMappedType(target);
const modifiers = getMappedTypeModifiers(target);
Expand All @@ -17411,7 +17411,7 @@ namespace ts {
(<IndexedAccessType>template).indexType === getTypeParameterFromMappedType(target)) {
return Ternary.True;
}
if (!isGenericMappedType(source)) {
if (!isGenericMappedType(source) && !target.declaration.nameType) {
const targetConstraint = getConstraintTypeFromMappedType(target);
const sourceKeys = getIndexType(source, /*stringsOnly*/ undefined, /*noIndexSignatures*/ true);
const includeOptional = modifiers & MappedTypeModifiers.IncludeOptional;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,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> = {
[P in keyof T as P extends K ? never : P]: T[P];
};

/**
* 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"> & { b: string; }
>a : string
>obj : T

let { a, ...rest } = obj;
>a : string
>rest : Pick<T, Exclude<keyof T, "a">>
>rest : Omit<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"> & { b: string; }
>rest : Omit<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"> & { b: string; }
>test : <T extends { a: string; }>(obj: T) => Omit<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">
>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">
>obj : T

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

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

let { [a]: a5, ...r5 } = obj;
>a : "a"
>a5 : string
>r5 : Pick<T, Exclude<keyof T, "a">>
>r5 : Omit<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>
>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>
>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>
>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">
>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">
>a : string
>'hello' : "hello"
>rest : Pick<T, Exclude<keyof T, "a">>
>rest : Omit<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">
>targetProps : T

rest.foo;
>rest.foo : T["foo"]
>rest : Pick<T, Exclude<keyof T, "bar">>
>rest : Omit<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">
>x : any
>y : any
>t : T

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

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

let rest: { b: string }
Expand Down
18 changes: 9 additions & 9 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">

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

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

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

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

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

x.d = d;
>x.d = d : unknown
>x.d : unknown
>x : Pick<A, "b" | "c" | "d">
>x : Omit<A, "a">
>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">'.
tests/cases/compiler/omitTypeTestErrors01.ts(15,16): error TS2339: Property 'b' does not exist on type 'Omit<Foo, "c" | "b">'.


==== 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">'.
}

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">'.
}


12 changes: 6 additions & 6 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">

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

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

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

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

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

Expand Down
12 changes: 6 additions & 6 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">

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

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">>
>withRouter : <P extends RouteComponentProps, C extends ComponentType<P>>(component: (C & FunctionComponent<P>) | (C & ComponentClass<P>)) => ComponentClass<Omit<P, "route">>
>MyComponent : ComponentType<Props>

// Repro from #33490
Expand Down