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

Use homomorphic templated type for Omit #53110

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: 3 additions & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,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 Exclude<P, K>]: T[P];
};

/**
* Exclude null and undefined from T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class A {
>A : Symbol(A, Decl(destructuringUnspreadableIntoRest.ts, 0, 0))

rest1.publicProp;
>rest1.publicProp : Symbol(publicProp, Decl(destructuringUnspreadableIntoRest.ts, 1, 16))
>rest1.publicProp : Symbol(publicProp)
>rest1 : Symbol(rest1, Decl(destructuringUnspreadableIntoRest.ts, 14, 15))
>publicProp : Symbol(publicProp, Decl(destructuringUnspreadableIntoRest.ts, 1, 16))
>publicProp : Symbol(publicProp)

rest2.publicProp;
>rest2.publicProp : Symbol(A.publicProp, Decl(destructuringUnspreadableIntoRest.ts, 1, 16))
Expand Down Expand Up @@ -157,9 +157,9 @@ function destructure<T extends A>(x: T) {
>A : Symbol(A, Decl(destructuringUnspreadableIntoRest.ts, 0, 0))

rest1.publicProp;
>rest1.publicProp : Symbol(publicProp, Decl(destructuringUnspreadableIntoRest.ts, 1, 16))
>rest1.publicProp : Symbol(publicProp)
>rest1 : Symbol(rest1, Decl(destructuringUnspreadableIntoRest.ts, 52, 11))
>publicProp : Symbol(publicProp, Decl(destructuringUnspreadableIntoRest.ts, 1, 16))
>publicProp : Symbol(publicProp)

rest2.publicProp;
>rest2.publicProp : Symbol(A.publicProp, Decl(destructuringUnspreadableIntoRest.ts, 1, 16))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ switch (params.tag) {
// TS 4.3: string | number
const result = getType(params).type;
>result : Symbol(result, Decl(genericObjectSpreadResultInSwitch.ts, 21, 13))
>getType(params).type : Symbol(type, Decl(genericObjectSpreadResultInSwitch.ts, 2, 16))
>getType(params).type : Symbol(type)
>getType : Symbol(getType, Decl(genericObjectSpreadResultInSwitch.ts, 4, 5))
>params : Symbol(params, Decl(genericObjectSpreadResultInSwitch.ts, 15, 13))
>type : Symbol(type, Decl(genericObjectSpreadResultInSwitch.ts, 2, 16))
>type : Symbol(type)

break;
}
Expand All @@ -60,10 +60,10 @@ switch (params.tag) {
// TS 4.3: string | number
const result = getType(params).type;
>result : Symbol(result, Decl(genericObjectSpreadResultInSwitch.ts, 28, 13))
>getType(params).type : Symbol(type, Decl(genericObjectSpreadResultInSwitch.ts, 2, 45))
>getType(params).type : Symbol(type)
>getType : Symbol(getType, Decl(genericObjectSpreadResultInSwitch.ts, 4, 5))
>params : Symbol(params, Decl(genericObjectSpreadResultInSwitch.ts, 15, 13))
>type : Symbol(type, Decl(genericObjectSpreadResultInSwitch.ts, 2, 45))
>type : Symbol(type)

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class Model<Attributes = any> {
}

type ModelAttributes<T> = Omit<T, keyof Model>;
>ModelAttributes : ModelAttributes<T>
>ModelAttributes : Omit<T, "createdAt">

class AutoModel<T> extends Model<ModelAttributes<T>> {}
>AutoModel : AutoModel<T>
>Model : Model<ModelAttributes<T>>
>Model : Model<Omit<T, "createdAt">>

class PersonModel extends AutoModel<PersonModel> {
>PersonModel : PersonModel
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/mappedTypeConstraints.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ const modifier = <T extends TargetProps>(targetProps: T) => {
>targetProps : Symbol(targetProps, Decl(mappedTypeConstraints.ts, 30, 41))

rest.foo;
>rest.foo : Symbol(foo, Decl(mappedTypeConstraints.ts, 25, 20))
>rest.foo : Symbol(foo)
>rest : Symbol(rest, Decl(mappedTypeConstraints.ts, 31, 13))
>foo : Symbol(foo, Decl(mappedTypeConstraints.ts, 25, 20))
>foo : Symbol(foo)

};

13 changes: 13 additions & 0 deletions tests/baselines/reference/omitOfTypeExtendingIndexSignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [omitOfTypeExtendingIndexSignature.ts]
// #36981
type AnyRecord = Record<string, any>;
interface ExtendsAny extends AnyRecord {
myKey1: string;
myKey2: string;
}

type OmitsKey = Omit<ExtendsAny, "myKey2">;
type OmitsKey1 = OmitsKey["myKey1"]; // should be `string`
type OmitsKey2 = OmitsKey["myKey2"]; // should be `any`

//// [omitOfTypeExtendingIndexSignature.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/omitOfTypeExtendingIndexSignature.ts ===
// #36981
type AnyRecord = Record<string, any>;
>AnyRecord : Symbol(AnyRecord, Decl(omitOfTypeExtendingIndexSignature.ts, 0, 0))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))

interface ExtendsAny extends AnyRecord {
>ExtendsAny : Symbol(ExtendsAny, Decl(omitOfTypeExtendingIndexSignature.ts, 1, 37))
>AnyRecord : Symbol(AnyRecord, Decl(omitOfTypeExtendingIndexSignature.ts, 0, 0))

myKey1: string;
>myKey1 : Symbol(ExtendsAny.myKey1, Decl(omitOfTypeExtendingIndexSignature.ts, 2, 40))

myKey2: string;
>myKey2 : Symbol(ExtendsAny.myKey2, Decl(omitOfTypeExtendingIndexSignature.ts, 3, 19))
}

type OmitsKey = Omit<ExtendsAny, "myKey2">;
>OmitsKey : Symbol(OmitsKey, Decl(omitOfTypeExtendingIndexSignature.ts, 5, 1))
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
>ExtendsAny : Symbol(ExtendsAny, Decl(omitOfTypeExtendingIndexSignature.ts, 1, 37))

type OmitsKey1 = OmitsKey["myKey1"]; // should be `string`
>OmitsKey1 : Symbol(OmitsKey1, Decl(omitOfTypeExtendingIndexSignature.ts, 7, 43))
>OmitsKey : Symbol(OmitsKey, Decl(omitOfTypeExtendingIndexSignature.ts, 5, 1))

type OmitsKey2 = OmitsKey["myKey2"]; // should be `any`
>OmitsKey2 : Symbol(OmitsKey2, Decl(omitOfTypeExtendingIndexSignature.ts, 8, 36))
>OmitsKey : Symbol(OmitsKey, Decl(omitOfTypeExtendingIndexSignature.ts, 5, 1))

22 changes: 22 additions & 0 deletions tests/baselines/reference/omitOfTypeExtendingIndexSignature.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/omitOfTypeExtendingIndexSignature.ts ===
// #36981
type AnyRecord = Record<string, any>;
>AnyRecord : { [x: string]: any; }

interface ExtendsAny extends AnyRecord {
myKey1: string;
>myKey1 : string

myKey2: string;
>myKey2 : string
}

type OmitsKey = Omit<ExtendsAny, "myKey2">;
>OmitsKey : Omit<ExtendsAny, "myKey2">

type OmitsKey1 = OmitsKey["myKey1"]; // should be `string`
>OmitsKey1 : string

type OmitsKey2 = OmitsKey["myKey2"]; // should be `any`
>OmitsKey2 : any

28 changes: 14 additions & 14 deletions tests/baselines/reference/omitTypeHelperModifiers01.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@ function f(x: B) {

const b = x.b;
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 10, 9))
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
>x.b : Symbol(b)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
>b : Symbol(b)

x.b = "hello";
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
>x.b : Symbol(b)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
>b : Symbol(b)

x.b = undefined;
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
>x.b : Symbol(b)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
>b : Symbol(b)
>undefined : Symbol(undefined)

const c = x.c;
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 14, 9))
>x.c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
>x.c : Symbol(c)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
>c : Symbol(c)

x.c = true;
>x.c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
>x.c : Symbol(c)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
>c : Symbol(c)

const d = x.d;
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 17, 9))
>x.d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
>x.d : Symbol(d)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
>d : Symbol(d)

x.d = d;
>x.d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
>x.d : Symbol(d)
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
>d : Symbol(d)
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 17, 9))
}

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 : { b?: string | undefined; readonly c: boolean; d: unknown; }
>B : Omit<A, "a">

function f(x: B) {
>f : (x: B) => void
>x : B
>x : Omit<A, "a">

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

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

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

const c = x.c;
>c : boolean
>x.c : boolean
>x : B
>x : Omit<A, "a">
>c : boolean

x.c = true;
>x.c = true : true
>x.c : any
>x : B
>x : Omit<A, "a">
>c : any
>true : true

const d = x.d;
>d : unknown
>x.d : unknown
>x : B
>x : Omit<A, "a">
>d : unknown

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

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

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

return bar.c;
>bar.c : any
>bar : Bar
>bar : Omit<Foo, "c">
>c : any
}

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

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

Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/omitTypeTests01.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export function getBarA(bar: Bar) {
>Bar : Symbol(Bar, Decl(omitTypeTests01.ts, 4, 1))

return bar.a;
>bar.a : Symbol(a, Decl(omitTypeTests01.ts, 0, 15))
>bar.a : Symbol(a)
>bar : Symbol(bar, Decl(omitTypeTests01.ts, 9, 24))
>a : Symbol(a, Decl(omitTypeTests01.ts, 0, 15))
>a : Symbol(a)
}

export function getBazA(baz: Baz) {
Expand All @@ -39,9 +39,9 @@ export function getBazA(baz: Baz) {
>Baz : Symbol(Baz, Decl(omitTypeTests01.ts, 6, 33))

return baz.a;
>baz.a : Symbol(a, Decl(omitTypeTests01.ts, 0, 15))
>baz.a : Symbol(a)
>baz : Symbol(baz, Decl(omitTypeTests01.ts, 13, 24))
>a : Symbol(a, Decl(omitTypeTests01.ts, 0, 15))
>a : Symbol(a)
}


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 : { a: string; b: number; }
>Bar : Omit<Foo, "c">

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

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

return bar.a;
>bar.a : string
>bar : Bar
>bar : Omit<Foo, "c">
>a : string
}

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

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

Expand Down
Loading