Skip to content

Commit 39e9a2b

Browse files
Maintain modifiers on Omit (#31205)
Maintain modifiers on `Omit`
2 parents 0c9db71 + d7434a0 commit 39e9a2b

16 files changed

+273
-51
lines changed

src/lib/es5.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,9 +1446,7 @@ 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> = {
1450-
[P in Exclude<keyof T, K>]: T[P]
1451-
};
1449+
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
14521450

14531451
/**
14541452
* Exclude null and undefined from T

tests/baselines/reference/genericIsNeverEmptyObject.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
// Repro from #29067
33

44
function test<T extends { a: string }>(obj: T) {
5-
>test : <T extends { a: string; }>(obj: T) => Omit<T, "a"> & { b: string; }
5+
>test : <T extends { a: string; }>(obj: T) => Pick<T, Exclude<keyof T, "a">> & { b: string; }
66
>a : string
77
>obj : T
88

99
let { a, ...rest } = obj;
1010
>a : string
11-
>rest : Omit<T, "a">
11+
>rest : Pick<T, Exclude<keyof T, "a">>
1212
>obj : T
1313

1414
return { ...rest, b: a };
15-
>{ ...rest, b: a } : Omit<T, "a"> & { b: string; }
16-
>rest : Omit<T, "a">
15+
>{ ...rest, b: a } : Pick<T, Exclude<keyof T, "a">> & { b: string; }
16+
>rest : Pick<T, Exclude<keyof T, "a">>
1717
>b : string
1818
>a : string
1919
}
@@ -30,7 +30,7 @@ let o2: { b: string, x: number } = test(o1);
3030
>o2 : { b: string; x: number; }
3131
>b : string
3232
>x : number
33-
>test(o1) : Omit<{ a: string; x: number; }, "a"> & { b: string; }
34-
>test : <T extends { a: string; }>(obj: T) => Omit<T, "a"> & { b: string; }
33+
>test(o1) : Pick<{ a: string; x: number; }, "x"> & { b: string; }
34+
>test : <T extends { a: string; }>(obj: T) => Pick<T, Exclude<keyof T, "a">> & { b: string; }
3535
>o1 : { a: string; x: number; }
3636

tests/baselines/reference/genericObjectRest.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@ function f1<T extends { a: string, b: number }>(obj: T) {
1616
let { a: a1, ...r1 } = obj;
1717
>a : any
1818
>a1 : string
19-
>r1 : Omit<T, "a">
19+
>r1 : Pick<T, Exclude<keyof T, "a">>
2020
>obj : T
2121

2222
let { a: a2, b: b2, ...r2 } = obj;
2323
>a : any
2424
>a2 : string
2525
>b : any
2626
>b2 : number
27-
>r2 : Omit<T, "a" | "b">
27+
>r2 : Pick<T, Exclude<keyof T, "a" | "b">>
2828
>obj : T
2929

3030
let { 'a': a3, ...r3 } = obj;
3131
>a3 : string
32-
>r3 : Omit<T, "a">
32+
>r3 : Pick<T, Exclude<keyof T, "a">>
3333
>obj : T
3434

3535
let { ['a']: a4, ...r4 } = obj;
3636
>'a' : "a"
3737
>a4 : string
38-
>r4 : Omit<T, "a">
38+
>r4 : Pick<T, Exclude<keyof T, "a">>
3939
>obj : T
4040

4141
let { [a]: a5, ...r5 } = obj;
4242
>a : "a"
4343
>a5 : string
44-
>r5 : Omit<T, "a">
44+
>r5 : Pick<T, Exclude<keyof T, "a">>
4545
>obj : T
4646
}
4747

@@ -68,7 +68,7 @@ function f2<T extends { [sa]: string, [sb]: number }>(obj: T) {
6868
>a1 : string
6969
>sb : unique symbol
7070
>b1 : number
71-
>r1 : Omit<T, unique symbol | unique symbol>
71+
>r1 : Pick<T, Exclude<keyof T, unique symbol | unique symbol>>
7272
>obj : T
7373
}
7474

@@ -83,7 +83,7 @@ function f3<T, K1 extends keyof T, K2 extends keyof T>(obj: T, k1: K1, k2: K2) {
8383
>a1 : T[K1]
8484
>k2 : K2
8585
>a2 : T[K2]
86-
>r1 : Omit<T, K1 | K2>
86+
>r1 : Pick<T, Exclude<keyof T, K1 | K2>>
8787
>obj : T
8888
}
8989

@@ -104,7 +104,7 @@ function f4<K1 extends keyof Item, K2 extends keyof Item>(obj: Item, k1: K1, k2:
104104
>a1 : Item[K1]
105105
>k2 : K2
106106
>a2 : Item[K2]
107-
>r1 : Omit<Item, K1 | K2>
107+
>r1 : Pick<Item, Exclude<"a", K1 | K2> | Exclude<"b", K1 | K2> | Exclude<"c", K1 | K2>>
108108
>obj : Item
109109
}
110110

tests/baselines/reference/literalTypeWidening.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,14 @@ function test<T extends { a: string, b: string }>(obj: T): T {
443443

444444
let { a, ...rest } = obj;
445445
>a : string
446-
>rest : Omit<T, "a">
446+
>rest : Pick<T, Exclude<keyof T, "a">>
447447
>obj : T
448448

449449
return { a: 'hello', ...rest } as T;
450450
>{ a: 'hello', ...rest } as T : T
451-
>{ a: 'hello', ...rest } : { a: string; } & Omit<T, "a">
451+
>{ a: 'hello', ...rest } : { a: string; } & Pick<T, Exclude<keyof T, "a">>
452452
>a : string
453453
>'hello' : "hello"
454-
>rest : Omit<T, "a">
454+
>rest : Pick<T, Exclude<keyof T, "a">>
455455
}
456456

tests/baselines/reference/mappedTypeConstraints.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ const modifier = <T extends TargetProps>(targetProps: T) => {
132132
>targetProps : Symbol(targetProps, Decl(mappedTypeConstraints.ts, 30, 41))
133133

134134
rest.foo;
135-
>rest.foo : Symbol(foo)
135+
>rest.foo : Symbol(foo, Decl(mappedTypeConstraints.ts, 25, 20))
136136
>rest : Symbol(rest, Decl(mappedTypeConstraints.ts, 31, 13))
137-
>foo : Symbol(foo)
137+
>foo : Symbol(foo, Decl(mappedTypeConstraints.ts, 25, 20))
138138

139139
};
140140

tests/baselines/reference/mappedTypeConstraints.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ const modifier = <T extends TargetProps>(targetProps: T) => {
9898

9999
let {bar, ...rest} = targetProps;
100100
>bar : string
101-
>rest : Omit<T, "bar">
101+
>rest : Pick<T, Exclude<keyof T, "bar">>
102102
>targetProps : T
103103

104104
rest.foo;
105105
>rest.foo : T["foo"]
106-
>rest : Omit<T, "bar">
106+
>rest : Pick<T, Exclude<keyof T, "bar">>
107107
>foo : T["foo"]
108108

109109
};

tests/baselines/reference/objectRestNegative.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ function stillMustBeLast({ ...mustBeLast, a }: { a: number, b: string }): void {
3636
>b : string
3737
}
3838
function generic<T extends { x, y }>(t: T) {
39-
>generic : <T extends { x: any; y: any; }>(t: T) => Omit<T, "x">
39+
>generic : <T extends { x: any; y: any; }>(t: T) => Pick<T, Exclude<keyof T, "x">>
4040
>x : any
4141
>y : any
4242
>t : T
4343

4444
let { x, ...rest } = t;
4545
>x : any
46-
>rest : Omit<T, "x">
46+
>rest : Pick<T, Exclude<keyof T, "x">>
4747
>t : T
4848

4949
return rest;
50-
>rest : Omit<T, "x">
50+
>rest : Pick<T, Exclude<keyof T, "x">>
5151
}
5252

5353
let rest: { b: string }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/omitTypeHelperModifiers01.ts(16,7): error TS2540: Cannot assign to 'c' because it is a read-only property.
2+
3+
4+
==== tests/cases/compiler/omitTypeHelperModifiers01.ts (1 errors) ====
5+
type A = {
6+
a: number;
7+
b?: string;
8+
readonly c: boolean;
9+
d: unknown;
10+
};
11+
12+
type B = Omit<A, 'a'>;
13+
14+
function f(x: B) {
15+
const b = x.b;
16+
x.b = "hello";
17+
x.b = undefined;
18+
19+
const c = x.c;
20+
x.c = true;
21+
~
22+
!!! error TS2540: Cannot assign to 'c' because it is a read-only property.
23+
24+
const d = x.d;
25+
x.d = d;
26+
}
27+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [omitTypeHelperModifiers01.ts]
2+
type A = {
3+
a: number;
4+
b?: string;
5+
readonly c: boolean;
6+
d: unknown;
7+
};
8+
9+
type B = Omit<A, 'a'>;
10+
11+
function f(x: B) {
12+
const b = x.b;
13+
x.b = "hello";
14+
x.b = undefined;
15+
16+
const c = x.c;
17+
x.c = true;
18+
19+
const d = x.d;
20+
x.d = d;
21+
}
22+
23+
24+
//// [omitTypeHelperModifiers01.js]
25+
"use strict";
26+
function f(x) {
27+
var b = x.b;
28+
x.b = "hello";
29+
x.b = undefined;
30+
var c = x.c;
31+
x.c = true;
32+
var d = x.d;
33+
x.d = d;
34+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
=== tests/cases/compiler/omitTypeHelperModifiers01.ts ===
2+
type A = {
3+
>A : Symbol(A, Decl(omitTypeHelperModifiers01.ts, 0, 0))
4+
5+
a: number;
6+
>a : Symbol(a, Decl(omitTypeHelperModifiers01.ts, 0, 10))
7+
8+
b?: string;
9+
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
10+
11+
readonly c: boolean;
12+
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
13+
14+
d: unknown;
15+
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
16+
17+
};
18+
19+
type B = Omit<A, 'a'>;
20+
>B : Symbol(B, Decl(omitTypeHelperModifiers01.ts, 5, 2))
21+
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
22+
>A : Symbol(A, Decl(omitTypeHelperModifiers01.ts, 0, 0))
23+
24+
function f(x: B) {
25+
>f : Symbol(f, Decl(omitTypeHelperModifiers01.ts, 7, 22))
26+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
27+
>B : Symbol(B, Decl(omitTypeHelperModifiers01.ts, 5, 2))
28+
29+
const b = x.b;
30+
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 10, 9))
31+
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
32+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
33+
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
34+
35+
x.b = "hello";
36+
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
37+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
38+
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
39+
40+
x.b = undefined;
41+
>x.b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
42+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
43+
>b : Symbol(b, Decl(omitTypeHelperModifiers01.ts, 1, 14))
44+
>undefined : Symbol(undefined)
45+
46+
const c = x.c;
47+
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 14, 9))
48+
>x.c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
49+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
50+
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
51+
52+
x.c = true;
53+
>x.c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
54+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
55+
>c : Symbol(c, Decl(omitTypeHelperModifiers01.ts, 2, 15))
56+
57+
const d = x.d;
58+
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 17, 9))
59+
>x.d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
60+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
61+
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
62+
63+
x.d = d;
64+
>x.d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
65+
>x : Symbol(x, Decl(omitTypeHelperModifiers01.ts, 9, 11))
66+
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 3, 24))
67+
>d : Symbol(d, Decl(omitTypeHelperModifiers01.ts, 17, 9))
68+
}
69+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
=== tests/cases/compiler/omitTypeHelperModifiers01.ts ===
2+
type A = {
3+
>A : A
4+
5+
a: number;
6+
>a : number
7+
8+
b?: string;
9+
>b : string | undefined
10+
11+
readonly c: boolean;
12+
>c : boolean
13+
14+
d: unknown;
15+
>d : unknown
16+
17+
};
18+
19+
type B = Omit<A, 'a'>;
20+
>B : Pick<A, "b" | "c" | "d">
21+
22+
function f(x: B) {
23+
>f : (x: Pick<A, "b" | "c" | "d">) => void
24+
>x : Pick<A, "b" | "c" | "d">
25+
26+
const b = x.b;
27+
>b : string | undefined
28+
>x.b : string | undefined
29+
>x : Pick<A, "b" | "c" | "d">
30+
>b : string | undefined
31+
32+
x.b = "hello";
33+
>x.b = "hello" : "hello"
34+
>x.b : string | undefined
35+
>x : Pick<A, "b" | "c" | "d">
36+
>b : string | undefined
37+
>"hello" : "hello"
38+
39+
x.b = undefined;
40+
>x.b = undefined : undefined
41+
>x.b : string | undefined
42+
>x : Pick<A, "b" | "c" | "d">
43+
>b : string | undefined
44+
>undefined : undefined
45+
46+
const c = x.c;
47+
>c : boolean
48+
>x.c : boolean
49+
>x : Pick<A, "b" | "c" | "d">
50+
>c : boolean
51+
52+
x.c = true;
53+
>x.c = true : true
54+
>x.c : any
55+
>x : Pick<A, "b" | "c" | "d">
56+
>c : any
57+
>true : true
58+
59+
const d = x.d;
60+
>d : unknown
61+
>x.d : unknown
62+
>x : Pick<A, "b" | "c" | "d">
63+
>d : unknown
64+
65+
x.d = d;
66+
>x.d = d : unknown
67+
>x.d : unknown
68+
>x : Pick<A, "b" | "c" | "d">
69+
>d : unknown
70+
>d : unknown
71+
}
72+

0 commit comments

Comments
 (0)