Skip to content

Commit 0c9a35c

Browse files
rpgeeganageRyanCavanaugh
authored andcommitted
Use 'Omit' instead of 'Pick<Exclude<...>>' for object rest (#31134)
* add Omit<T, ..> instead of Pick<Exclue<T>,..> * remove the fallback * run the baseline-accept * removed unused variables * fix tests\baselines\reference
1 parent 49d6f61 commit 0c9a35c

File tree

7 files changed

+31
-37
lines changed

7 files changed

+31
-37
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ namespace ts {
535535
let deferredGlobalTemplateStringsArrayType: ObjectType;
536536
let deferredGlobalImportMetaType: ObjectType;
537537
let deferredGlobalExtractSymbol: Symbol;
538-
let deferredGlobalExcludeSymbol: Symbol;
539-
let deferredGlobalPickSymbol: Symbol;
538+
let deferredGlobalOmitSymbol: Symbol;
540539
let deferredGlobalBigIntType: ObjectType;
541540

542541
const allPotentiallyUnusedIdentifiers = createMap<PotentiallyUnusedIdentifier[]>(); // key is file name
@@ -4969,13 +4968,12 @@ namespace ts {
49694968
if (omitKeyType.flags & TypeFlags.Never) {
49704969
return source;
49714970
}
4972-
const pickTypeAlias = getGlobalPickSymbol();
4973-
const excludeTypeAlias = getGlobalExcludeSymbol();
4974-
if (!pickTypeAlias || !excludeTypeAlias) {
4971+
4972+
const omitTypeAlias = getGlobalOmitSymbol();
4973+
if (!omitTypeAlias) {
49754974
return errorType;
49764975
}
4977-
const pickKeys = getTypeAliasInstantiation(excludeTypeAlias, [getIndexType(source), omitKeyType]);
4978-
return getTypeAliasInstantiation(pickTypeAlias, [source, pickKeys]);
4976+
return getTypeAliasInstantiation(omitTypeAlias, [source, omitKeyType]);
49794977
}
49804978
const members = createSymbolTable();
49814979
for (const prop of getPropertiesOfType(source)) {
@@ -9305,12 +9303,8 @@ namespace ts {
93059303
return deferredGlobalExtractSymbol || (deferredGlobalExtractSymbol = getGlobalSymbol("Extract" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
93069304
}
93079305

9308-
function getGlobalExcludeSymbol(): Symbol {
9309-
return deferredGlobalExcludeSymbol || (deferredGlobalExcludeSymbol = getGlobalSymbol("Exclude" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
9310-
}
9311-
9312-
function getGlobalPickSymbol(): Symbol {
9313-
return deferredGlobalPickSymbol || (deferredGlobalPickSymbol = getGlobalSymbol("Pick" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
9306+
function getGlobalOmitSymbol(): Symbol {
9307+
return deferredGlobalOmitSymbol || (deferredGlobalOmitSymbol = getGlobalSymbol("Omit" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
93149308
}
93159309

93169310
function getGlobalBigIntType(reportErrors: boolean) {

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

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

1414
return { ...rest, b: a };
15-
>{ ...rest, b: a } : Pick<T, Exclude<keyof T, "a">> & { b: string; }
16-
>rest : Pick<T, Exclude<keyof T, "a">>
15+
>{ ...rest, b: a } : Omit<T, "a"> & { b: string; }
16+
>rest : Omit<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) : Pick<{ a: string; x: number; }, "x"> & { b: string; }
34-
>test : <T extends { a: string; }>(obj: T) => Pick<T, Exclude<keyof T, "a">> & { b: string; }
33+
>test(o1) : Omit<{ a: string; x: number; }, "a"> & { b: string; }
34+
>test : <T extends { a: string; }>(obj: T) => Omit<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 : Pick<T, Exclude<keyof T, "a">>
19+
>r1 : Omit<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 : Pick<T, Exclude<keyof T, "a" | "b">>
27+
>r2 : Omit<T, "a" | "b">
2828
>obj : T
2929

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

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

4141
let { [a]: a5, ...r5 } = obj;
4242
>a : "a"
4343
>a5 : string
44-
>r5 : Pick<T, Exclude<keyof T, "a">>
44+
>r5 : Omit<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 : Pick<T, Exclude<keyof T, unique symbol | unique symbol>>
71+
>r1 : Omit<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 : Pick<T, Exclude<keyof T, K1 | K2>>
86+
>r1 : Omit<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 : Pick<Item, Exclude<"a", K1 | K2> | Exclude<"b", K1 | K2> | Exclude<"c", K1 | K2>>
107+
>r1 : Omit<Item, 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 : Pick<T, Exclude<keyof T, "a">>
446+
>rest : Omit<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; } & Pick<T, Exclude<keyof T, "a">>
451+
>{ a: 'hello', ...rest } : { a: string; } & Omit<T, "a">
452452
>a : string
453453
>'hello' : "hello"
454-
>rest : Pick<T, Exclude<keyof T, "a">>
454+
>rest : Omit<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, Decl(mappedTypeConstraints.ts, 25, 20))
135+
>rest.foo : Symbol(foo)
136136
>rest : Symbol(rest, Decl(mappedTypeConstraints.ts, 31, 13))
137-
>foo : Symbol(foo, Decl(mappedTypeConstraints.ts, 25, 20))
137+
>foo : Symbol(foo)
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 : Pick<T, Exclude<keyof T, "bar">>
101+
>rest : Omit<T, "bar">
102102
>targetProps : T
103103

104104
rest.foo;
105105
>rest.foo : T["foo"]
106-
>rest : Pick<T, Exclude<keyof T, "bar">>
106+
>rest : Omit<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) => Pick<T, Exclude<keyof T, "x">>
39+
>generic : <T extends { x: any; y: any; }>(t: T) => Omit<T, "x">
4040
>x : any
4141
>y : any
4242
>t : T
4343

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

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

5353
let rest: { b: string }

0 commit comments

Comments
 (0)