Skip to content

Commit 9fd7d0a

Browse files
committed
Remove Rest<T, K> and use Pick<T, Exclude<keyof T, K>> instead
1 parent f558059 commit 9fd7d0a

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ namespace ts {
517517
let deferredGlobalTemplateStringsArrayType: ObjectType;
518518
let deferredGlobalImportMetaType: ObjectType;
519519
let deferredGlobalExtractSymbol: Symbol;
520-
let deferredGlobalRestSymbol: Symbol;
520+
let deferredGlobalExcludeSymbol: Symbol;
521+
let deferredGlobalPickSymbol: Symbol;
521522

522523
const allPotentiallyUnusedIdentifiers = createMap<PotentiallyUnusedIdentifier[]>(); // key is file name
523524

@@ -4602,10 +4603,16 @@ namespace ts {
46024603
}
46034604
const omitKeyType = getUnionType(map(properties, getLiteralTypeFromPropertyName));
46044605
if (isGenericObjectType(source) || isGenericIndexType(omitKeyType)) {
4605-
const restTypeAlias = getGlobalRestSymbol();
4606-
return !restTypeAlias ? errorType :
4607-
omitKeyType.flags & TypeFlags.Never ? source :
4608-
getTypeAliasInstantiation(restTypeAlias, [source, omitKeyType]);
4606+
if (omitKeyType.flags & TypeFlags.Never) {
4607+
return source;
4608+
}
4609+
const pickTypeAlias = getGlobalPickSymbol();
4610+
const excludeTypeAlias = getGlobalExcludeSymbol();
4611+
if (!pickTypeAlias || !excludeTypeAlias) {
4612+
return errorType;
4613+
}
4614+
const pickKeys = getTypeAliasInstantiation(excludeTypeAlias, [getIndexType(source), omitKeyType]);
4615+
return getTypeAliasInstantiation(pickTypeAlias, [source, pickKeys]);
46094616
}
46104617
const members = createSymbolTable();
46114618
for (const prop of getPropertiesOfType(source)) {
@@ -8640,8 +8647,12 @@ namespace ts {
86408647
return deferredGlobalExtractSymbol || (deferredGlobalExtractSymbol = getGlobalSymbol("Extract" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
86418648
}
86428649

8643-
function getGlobalRestSymbol(): Symbol {
8644-
return deferredGlobalRestSymbol || (deferredGlobalRestSymbol = getGlobalSymbol("Rest" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
8650+
function getGlobalExcludeSymbol(): Symbol {
8651+
return deferredGlobalExcludeSymbol || (deferredGlobalExcludeSymbol = getGlobalSymbol("Exclude" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
8652+
}
8653+
8654+
function getGlobalPickSymbol(): Symbol {
8655+
return deferredGlobalPickSymbol || (deferredGlobalPickSymbol = getGlobalSymbol("Pick" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
86458656
}
86468657

86478658
/**
@@ -9227,9 +9238,8 @@ namespace ts {
92279238
}
92289239

92299240
function getLiteralTypeFromPropertyName(name: PropertyName) {
9230-
return isComputedPropertyName(name) ? checkComputedPropertyName(name) :
9231-
isIdentifier(name) ? getLiteralType(unescapeLeadingUnderscores(name.escapedText)) :
9232-
checkExpression(name);
9241+
return isIdentifier(name) ? getLiteralType(unescapeLeadingUnderscores(name.escapedText)) :
9242+
getRegularTypeOfLiteralType(isComputedPropertyName(name) ? checkComputedPropertyName(name) : checkExpression(name));
92339243
}
92349244

92359245
function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags) {

src/lib/es5.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,11 +1438,6 @@ type Extract<T, U> = T extends U ? T : never;
14381438
*/
14391439
type NonNullable<T> = T extends null | undefined ? never : T;
14401440

1441-
/**
1442-
* From T, pick all properties except those in the union K
1443-
*/
1444-
type Rest<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
1445-
14461441
/**
14471442
* Obtain the parameters of a function type in a tuple
14481443
*/

0 commit comments

Comments
 (0)