@@ -517,6 +517,7 @@ namespace ts {
517
517
let deferredGlobalTemplateStringsArrayType: ObjectType;
518
518
let deferredGlobalImportMetaType: ObjectType;
519
519
let deferredGlobalExtractSymbol: Symbol;
520
+ let deferredGlobalRestSymbol: Symbol;
520
521
521
522
const allPotentiallyUnusedIdentifiers = createMap<PotentiallyUnusedIdentifier[]>(); // key is file name
522
523
@@ -4596,26 +4597,18 @@ namespace ts {
4596
4597
if (source.flags & TypeFlags.Never) {
4597
4598
return emptyObjectType;
4598
4599
}
4599
-
4600
4600
if (source.flags & TypeFlags.Union) {
4601
4601
return mapType(source, t => getRestType(t, properties, symbol));
4602
4602
}
4603
-
4604
- const members = createSymbolTable();
4605
- const names = createUnderscoreEscapedMap<true>();
4606
- for (const name of properties) {
4607
- names.set(getTextOfPropertyName(name), true);
4603
+ const restTypeAlias = getGlobalRestSymbol();
4604
+ if (!restTypeAlias) {
4605
+ return errorType;
4608
4606
}
4609
- for (const prop of getPropertiesOfType(source)) {
4610
- if (!names.has(prop.escapedName)
4611
- && !(getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected))
4612
- && isSpreadableProperty(prop)) {
4613
- members.set(prop.escapedName, getSpreadSymbol(prop));
4614
- }
4607
+ const omitTypes = getUnionType(map(properties, getLiteralTypeFromPropertyName));
4608
+ if (omitTypes.flags & TypeFlags.Never) {
4609
+ return source;
4615
4610
}
4616
- const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String);
4617
- const numberIndexInfo = getIndexInfoOfType(source, IndexKind.Number);
4618
- return createAnonymousType(symbol, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
4611
+ return getTypeAliasInstantiation(restTypeAlias, [source, omitTypes]);
4619
4612
}
4620
4613
4621
4614
/** Return the inferred type for a binding element */
@@ -4639,7 +4632,7 @@ namespace ts {
4639
4632
let type: Type | undefined;
4640
4633
if (pattern.kind === SyntaxKind.ObjectBindingPattern) {
4641
4634
if (declaration.dotDotDotToken) {
4642
- if (parentType.flags & TypeFlags.Unknown || !isValidSpreadType(parentType) || isGenericObjectType(parentType) ) {
4635
+ if (parentType.flags & TypeFlags.Unknown || !isValidSpreadType(parentType)) {
4643
4636
error(declaration, Diagnostics.Rest_types_may_only_be_created_from_object_types);
4644
4637
return errorType;
4645
4638
}
@@ -6823,7 +6816,7 @@ namespace ts {
6823
6816
if (isMappedTypeWithKeyofConstraintDeclaration(type)) {
6824
6817
// We have a { [P in keyof T]: X }
6825
6818
for (const prop of getPropertiesOfType(modifiersType)) {
6826
- addMemberForKeyType(getLiteralTypeFromPropertyName (prop, include));
6819
+ addMemberForKeyType(getLiteralTypeFromProperty (prop, include));
6827
6820
}
6828
6821
if (modifiersType.flags & TypeFlags.Any || getIndexInfoOfType(modifiersType, IndexKind.String)) {
6829
6822
addMemberForKeyType(stringType);
@@ -8674,6 +8667,10 @@ namespace ts {
8674
8667
return deferredGlobalExtractSymbol || (deferredGlobalExtractSymbol = getGlobalSymbol("Extract" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
8675
8668
}
8676
8669
8670
+ function getGlobalRestSymbol(): Symbol {
8671
+ return deferredGlobalRestSymbol || (deferredGlobalRestSymbol = getGlobalSymbol("Rest" as __String, SymbolFlags.TypeAlias, Diagnostics.Cannot_find_global_type_0)!); // TODO: GH#18217
8672
+ }
8673
+
8677
8674
/**
8678
8675
* Instantiates a global type that is generic with some element type, and returns that instantiation.
8679
8676
*/
@@ -9256,14 +9253,18 @@ namespace ts {
9256
9253
type.resolvedIndexType || (type.resolvedIndexType = createIndexType(type, /*stringsOnly*/ false));
9257
9254
}
9258
9255
9259
- function getLiteralTypeFromPropertyName(prop: Symbol, include: TypeFlags) {
9256
+ function getLiteralTypeFromPropertyName(name: PropertyName) {
9257
+ return isComputedPropertyName(name) ? checkComputedPropertyName(name) :
9258
+ isIdentifier(name) ? getLiteralType(unescapeLeadingUnderscores(name.escapedText)) :
9259
+ checkExpression(name);
9260
+ }
9261
+
9262
+ function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags) {
9260
9263
if (!(getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier)) {
9261
9264
let type = getLateBoundSymbol(prop).nameType;
9262
9265
if (!type && !isKnownSymbol(prop)) {
9263
- const name = prop.valueDeclaration && getNameOfDeclaration(prop.valueDeclaration);
9264
- type = name && isNumericLiteral(name) ? getLiteralType(+name.text) :
9265
- name && name.kind === SyntaxKind.ComputedPropertyName && isNumericLiteral(name.expression) ? getLiteralType(+name.expression.text) :
9266
- getLiteralType(symbolName(prop));
9266
+ const name = prop.valueDeclaration && getNameOfDeclaration(prop.valueDeclaration) as PropertyName;
9267
+ type = name && getLiteralTypeFromPropertyName(name) || getLiteralType(symbolName(prop));
9267
9268
}
9268
9269
if (type && type.flags & include) {
9269
9270
return type;
@@ -9272,8 +9273,8 @@ namespace ts {
9272
9273
return neverType;
9273
9274
}
9274
9275
9275
- function getLiteralTypeFromPropertyNames (type: Type, include: TypeFlags) {
9276
- return getUnionType(map(getPropertiesOfType(type), t => getLiteralTypeFromPropertyName (t, include)));
9276
+ function getLiteralTypeFromProperties (type: Type, include: TypeFlags) {
9277
+ return getUnionType(map(getPropertiesOfType(type), t => getLiteralTypeFromProperty (t, include)));
9277
9278
}
9278
9279
9279
9280
function getNonEnumNumberIndexInfo(type: Type) {
@@ -9288,10 +9289,10 @@ namespace ts {
9288
9289
getObjectFlags(type) & ObjectFlags.Mapped ? getConstraintTypeFromMappedType(<MappedType>type) :
9289
9290
type === wildcardType ? wildcardType :
9290
9291
type.flags & TypeFlags.Any ? keyofConstraintType :
9291
- stringsOnly ? getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromPropertyNames (type, TypeFlags.StringLiteral) :
9292
- getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromPropertyNames (type, TypeFlags.UniqueESSymbol)]) :
9293
- getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromPropertyNames (type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
9294
- getLiteralTypeFromPropertyNames (type, TypeFlags.StringOrNumberLiteralOrUnique);
9292
+ stringsOnly ? getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromProperties (type, TypeFlags.StringLiteral) :
9293
+ getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromProperties (type, TypeFlags.UniqueESSymbol)]) :
9294
+ getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromProperties (type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
9295
+ getLiteralTypeFromProperties (type, TypeFlags.StringOrNumberLiteralOrUnique);
9295
9296
}
9296
9297
9297
9298
function getExtractStringType(type: Type) {
@@ -10976,7 +10977,7 @@ namespace ts {
10976
10977
if (!length(node.properties)) return;
10977
10978
for (const prop of node.properties) {
10978
10979
if (isSpreadAssignment(prop)) continue;
10979
- const type = getLiteralTypeFromPropertyName (getSymbolOfNode(prop), TypeFlags.StringOrNumberLiteralOrUnique);
10980
+ const type = getLiteralTypeFromProperty (getSymbolOfNode(prop), TypeFlags.StringOrNumberLiteralOrUnique);
10980
10981
if (!type || (type.flags & TypeFlags.Never)) {
10981
10982
continue;
10982
10983
}
0 commit comments