Skip to content

Commit 58898f4

Browse files
Make Omit alias a separate type (#31115)
Make `Omit` alias a separate type
2 parents b45df89 + a34f988 commit 58898f4

10 files changed

+324
-1
lines changed

src/lib/es5.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,9 @@ type Extract<T, U> = T extends U ? T : never;
14461446
/**
14471447
* Construct a type with the properties of T except for those in type K.
14481448
*/
1449-
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
1449+
type Omit<T, K extends keyof any> = {
1450+
[P in Exclude<keyof T, K>]: T[P]
1451+
};
14501452

14511453
/**
14521454
* Exclude null and undefined from T
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/omitTypeTestErrors01.ts(11,16): error TS2339: Property 'c' does not exist on type 'Omit<Foo, "c">'.
2+
tests/cases/compiler/omitTypeTestErrors01.ts(15,16): error TS2339: Property 'b' does not exist on type 'Omit<Foo, "c" | "b">'.
3+
4+
5+
==== tests/cases/compiler/omitTypeTestErrors01.ts (2 errors) ====
6+
interface Foo {
7+
a: string;
8+
b: number;
9+
c: boolean;
10+
}
11+
12+
export type Bar = Omit<Foo, "c">;
13+
export type Baz = Omit<Foo, "b" | "c">;
14+
15+
export function getBarC(bar: Bar) {
16+
return bar.c;
17+
~
18+
!!! error TS2339: Property 'c' does not exist on type 'Omit<Foo, "c">'.
19+
}
20+
21+
export function getBazB(baz: Baz) {
22+
return baz.b;
23+
~
24+
!!! error TS2339: Property 'b' does not exist on type 'Omit<Foo, "c" | "b">'.
25+
}
26+
27+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [omitTypeTestErrors01.ts]
2+
interface Foo {
3+
a: string;
4+
b: number;
5+
c: boolean;
6+
}
7+
8+
export type Bar = Omit<Foo, "c">;
9+
export type Baz = Omit<Foo, "b" | "c">;
10+
11+
export function getBarC(bar: Bar) {
12+
return bar.c;
13+
}
14+
15+
export function getBazB(baz: Baz) {
16+
return baz.b;
17+
}
18+
19+
20+
21+
//// [omitTypeTestErrors01.js]
22+
"use strict";
23+
exports.__esModule = true;
24+
function getBarC(bar) {
25+
return bar.c;
26+
}
27+
exports.getBarC = getBarC;
28+
function getBazB(baz) {
29+
return baz.b;
30+
}
31+
exports.getBazB = getBazB;
32+
33+
34+
//// [omitTypeTestErrors01.d.ts]
35+
interface Foo {
36+
a: string;
37+
b: number;
38+
c: boolean;
39+
}
40+
export declare type Bar = Omit<Foo, "c">;
41+
export declare type Baz = Omit<Foo, "b" | "c">;
42+
export declare function getBarC(bar: Bar): any;
43+
export declare function getBazB(baz: Baz): any;
44+
export {};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=== tests/cases/compiler/omitTypeTestErrors01.ts ===
2+
interface Foo {
3+
>Foo : Symbol(Foo, Decl(omitTypeTestErrors01.ts, 0, 0))
4+
5+
a: string;
6+
>a : Symbol(Foo.a, Decl(omitTypeTestErrors01.ts, 0, 15))
7+
8+
b: number;
9+
>b : Symbol(Foo.b, Decl(omitTypeTestErrors01.ts, 1, 14))
10+
11+
c: boolean;
12+
>c : Symbol(Foo.c, Decl(omitTypeTestErrors01.ts, 2, 14))
13+
}
14+
15+
export type Bar = Omit<Foo, "c">;
16+
>Bar : Symbol(Bar, Decl(omitTypeTestErrors01.ts, 4, 1))
17+
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
18+
>Foo : Symbol(Foo, Decl(omitTypeTestErrors01.ts, 0, 0))
19+
20+
export type Baz = Omit<Foo, "b" | "c">;
21+
>Baz : Symbol(Baz, Decl(omitTypeTestErrors01.ts, 6, 33))
22+
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
23+
>Foo : Symbol(Foo, Decl(omitTypeTestErrors01.ts, 0, 0))
24+
25+
export function getBarC(bar: Bar) {
26+
>getBarC : Symbol(getBarC, Decl(omitTypeTestErrors01.ts, 7, 39))
27+
>bar : Symbol(bar, Decl(omitTypeTestErrors01.ts, 9, 24))
28+
>Bar : Symbol(Bar, Decl(omitTypeTestErrors01.ts, 4, 1))
29+
30+
return bar.c;
31+
>bar : Symbol(bar, Decl(omitTypeTestErrors01.ts, 9, 24))
32+
}
33+
34+
export function getBazB(baz: Baz) {
35+
>getBazB : Symbol(getBazB, Decl(omitTypeTestErrors01.ts, 11, 1))
36+
>baz : Symbol(baz, Decl(omitTypeTestErrors01.ts, 13, 24))
37+
>Baz : Symbol(Baz, Decl(omitTypeTestErrors01.ts, 6, 33))
38+
39+
return baz.b;
40+
>baz : Symbol(baz, Decl(omitTypeTestErrors01.ts, 13, 24))
41+
}
42+
43+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/omitTypeTestErrors01.ts ===
2+
interface Foo {
3+
a: string;
4+
>a : string
5+
6+
b: number;
7+
>b : number
8+
9+
c: boolean;
10+
>c : boolean
11+
}
12+
13+
export type Bar = Omit<Foo, "c">;
14+
>Bar : Omit<Foo, "c">
15+
16+
export type Baz = Omit<Foo, "b" | "c">;
17+
>Baz : Omit<Foo, "c" | "b">
18+
19+
export function getBarC(bar: Bar) {
20+
>getBarC : (bar: Omit<Foo, "c">) => any
21+
>bar : Omit<Foo, "c">
22+
23+
return bar.c;
24+
>bar.c : any
25+
>bar : Omit<Foo, "c">
26+
>c : any
27+
}
28+
29+
export function getBazB(baz: Baz) {
30+
>getBazB : (baz: Omit<Foo, "c" | "b">) => any
31+
>baz : Omit<Foo, "c" | "b">
32+
33+
return baz.b;
34+
>baz.b : any
35+
>baz : Omit<Foo, "c" | "b">
36+
>b : any
37+
}
38+
39+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [omitTypeTests01.ts]
2+
interface Foo {
3+
a: string;
4+
b: number;
5+
c: boolean;
6+
}
7+
8+
export type Bar = Omit<Foo, "c">;
9+
export type Baz = Omit<Foo, "b" | "c">;
10+
11+
export function getBarA(bar: Bar) {
12+
return bar.a;
13+
}
14+
15+
export function getBazA(baz: Baz) {
16+
return baz.a;
17+
}
18+
19+
20+
21+
//// [omitTypeTests01.js]
22+
"use strict";
23+
exports.__esModule = true;
24+
function getBarA(bar) {
25+
return bar.a;
26+
}
27+
exports.getBarA = getBarA;
28+
function getBazA(baz) {
29+
return baz.a;
30+
}
31+
exports.getBazA = getBazA;
32+
33+
34+
//// [omitTypeTests01.d.ts]
35+
interface Foo {
36+
a: string;
37+
b: number;
38+
c: boolean;
39+
}
40+
export declare type Bar = Omit<Foo, "c">;
41+
export declare type Baz = Omit<Foo, "b" | "c">;
42+
export declare function getBarA(bar: Bar): string;
43+
export declare function getBazA(baz: Baz): string;
44+
export {};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/compiler/omitTypeTests01.ts ===
2+
interface Foo {
3+
>Foo : Symbol(Foo, Decl(omitTypeTests01.ts, 0, 0))
4+
5+
a: string;
6+
>a : Symbol(Foo.a, Decl(omitTypeTests01.ts, 0, 15))
7+
8+
b: number;
9+
>b : Symbol(Foo.b, Decl(omitTypeTests01.ts, 1, 14))
10+
11+
c: boolean;
12+
>c : Symbol(Foo.c, Decl(omitTypeTests01.ts, 2, 14))
13+
}
14+
15+
export type Bar = Omit<Foo, "c">;
16+
>Bar : Symbol(Bar, Decl(omitTypeTests01.ts, 4, 1))
17+
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
18+
>Foo : Symbol(Foo, Decl(omitTypeTests01.ts, 0, 0))
19+
20+
export type Baz = Omit<Foo, "b" | "c">;
21+
>Baz : Symbol(Baz, Decl(omitTypeTests01.ts, 6, 33))
22+
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
23+
>Foo : Symbol(Foo, Decl(omitTypeTests01.ts, 0, 0))
24+
25+
export function getBarA(bar: Bar) {
26+
>getBarA : Symbol(getBarA, Decl(omitTypeTests01.ts, 7, 39))
27+
>bar : Symbol(bar, Decl(omitTypeTests01.ts, 9, 24))
28+
>Bar : Symbol(Bar, Decl(omitTypeTests01.ts, 4, 1))
29+
30+
return bar.a;
31+
>bar.a : Symbol(a)
32+
>bar : Symbol(bar, Decl(omitTypeTests01.ts, 9, 24))
33+
>a : Symbol(a)
34+
}
35+
36+
export function getBazA(baz: Baz) {
37+
>getBazA : Symbol(getBazA, Decl(omitTypeTests01.ts, 11, 1))
38+
>baz : Symbol(baz, Decl(omitTypeTests01.ts, 13, 24))
39+
>Baz : Symbol(Baz, Decl(omitTypeTests01.ts, 6, 33))
40+
41+
return baz.a;
42+
>baz.a : Symbol(a)
43+
>baz : Symbol(baz, Decl(omitTypeTests01.ts, 13, 24))
44+
>a : Symbol(a)
45+
}
46+
47+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/omitTypeTests01.ts ===
2+
interface Foo {
3+
a: string;
4+
>a : string
5+
6+
b: number;
7+
>b : number
8+
9+
c: boolean;
10+
>c : boolean
11+
}
12+
13+
export type Bar = Omit<Foo, "c">;
14+
>Bar : Omit<Foo, "c">
15+
16+
export type Baz = Omit<Foo, "b" | "c">;
17+
>Baz : Omit<Foo, "c" | "b">
18+
19+
export function getBarA(bar: Bar) {
20+
>getBarA : (bar: Omit<Foo, "c">) => string
21+
>bar : Omit<Foo, "c">
22+
23+
return bar.a;
24+
>bar.a : string
25+
>bar : Omit<Foo, "c">
26+
>a : string
27+
}
28+
29+
export function getBazA(baz: Baz) {
30+
>getBazA : (baz: Omit<Foo, "c" | "b">) => string
31+
>baz : Omit<Foo, "c" | "b">
32+
33+
return baz.a;
34+
>baz.a : string
35+
>baz : Omit<Foo, "c" | "b">
36+
>a : string
37+
}
38+
39+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @declaration: true
2+
3+
interface Foo {
4+
a: string;
5+
b: number;
6+
c: boolean;
7+
}
8+
9+
export type Bar = Omit<Foo, "c">;
10+
export type Baz = Omit<Foo, "b" | "c">;
11+
12+
export function getBarC(bar: Bar) {
13+
return bar.c;
14+
}
15+
16+
export function getBazB(baz: Baz) {
17+
return baz.b;
18+
}
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @declaration: true
2+
3+
interface Foo {
4+
a: string;
5+
b: number;
6+
c: boolean;
7+
}
8+
9+
export type Bar = Omit<Foo, "c">;
10+
export type Baz = Omit<Foo, "b" | "c">;
11+
12+
export function getBarA(bar: Bar) {
13+
return bar.a;
14+
}
15+
16+
export function getBazA(baz: Baz) {
17+
return baz.a;
18+
}
19+

0 commit comments

Comments
 (0)