From 9fd7d0a5f4cbe901c87ac04c32489db69c654f3e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 2 Nov 2018 06:40:26 -0700 Subject: [PATCH] Remove Rest and use Pick> instead --- src/compiler/checker.ts | 30 ++++++++++++++++++++---------- src/lib/es5.d.ts | 5 ----- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b0bc866fded..e0f40cd2606 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -517,7 +517,8 @@ namespace ts { let deferredGlobalTemplateStringsArrayType: ObjectType; let deferredGlobalImportMetaType: ObjectType; let deferredGlobalExtractSymbol: Symbol; - let deferredGlobalRestSymbol: Symbol; + let deferredGlobalExcludeSymbol: Symbol; + let deferredGlobalPickSymbol: Symbol; const allPotentiallyUnusedIdentifiers = createMap(); // key is file name @@ -4602,10 +4603,16 @@ namespace ts { } const omitKeyType = getUnionType(map(properties, getLiteralTypeFromPropertyName)); if (isGenericObjectType(source) || isGenericIndexType(omitKeyType)) { - const restTypeAlias = getGlobalRestSymbol(); - return !restTypeAlias ? errorType : - omitKeyType.flags & TypeFlags.Never ? source : - getTypeAliasInstantiation(restTypeAlias, [source, omitKeyType]); + if (omitKeyType.flags & TypeFlags.Never) { + return source; + } + const pickTypeAlias = getGlobalPickSymbol(); + const excludeTypeAlias = getGlobalExcludeSymbol(); + if (!pickTypeAlias || !excludeTypeAlias) { + return errorType; + } + const pickKeys = getTypeAliasInstantiation(excludeTypeAlias, [getIndexType(source), omitKeyType]); + return getTypeAliasInstantiation(pickTypeAlias, [source, pickKeys]); } const members = createSymbolTable(); for (const prop of getPropertiesOfType(source)) { @@ -8640,8 +8647,12 @@ namespace ts { return deferredGlobalExtractSymbol || (deferredGlobalExtractSymbol = getGlobalSymbol("Extract" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217 } - function getGlobalRestSymbol(): Symbol { - return deferredGlobalRestSymbol || (deferredGlobalRestSymbol = getGlobalSymbol("Rest" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217 + function getGlobalExcludeSymbol(): Symbol { + return deferredGlobalExcludeSymbol || (deferredGlobalExcludeSymbol = getGlobalSymbol("Exclude" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217 + } + + function getGlobalPickSymbol(): Symbol { + return deferredGlobalPickSymbol || (deferredGlobalPickSymbol = getGlobalSymbol("Pick" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217 } /** @@ -9227,9 +9238,8 @@ namespace ts { } function getLiteralTypeFromPropertyName(name: PropertyName) { - return isComputedPropertyName(name) ? checkComputedPropertyName(name) : - isIdentifier(name) ? getLiteralType(unescapeLeadingUnderscores(name.escapedText)) : - checkExpression(name); + return isIdentifier(name) ? getLiteralType(unescapeLeadingUnderscores(name.escapedText)) : + getRegularTypeOfLiteralType(isComputedPropertyName(name) ? checkComputedPropertyName(name) : checkExpression(name)); } function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags) { diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 98b4e3c625f..c8e99f0d1db 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -1438,11 +1438,6 @@ type Extract = T extends U ? T : never; */ type NonNullable = T extends null | undefined ? never : T; -/** - * From T, pick all properties except those in the union K - */ -type Rest = Pick>; - /** * Obtain the parameters of a function type in a tuple */