Only create Rest<T, K> types when object and/or key type is generic

This commit is contained in:
Anders Hejlsberg 2018-11-01 17:49:49 -07:00
parent 0b194a2b34
commit 396642d03e

View File

@ -4600,15 +4600,24 @@ namespace ts {
if (source.flags & TypeFlags.Union) {
return mapType(source, t => getRestType(t, properties, symbol));
}
const restTypeAlias = getGlobalRestSymbol();
if (!restTypeAlias) {
return errorType;
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]);
}
const omitTypes = getUnionType(map(properties, getLiteralTypeFromPropertyName));
if (omitTypes.flags & TypeFlags.Never) {
return source;
const members = createSymbolTable();
for (const prop of getPropertiesOfType(source)) {
if (!isTypeAssignableTo(getLiteralTypeFromProperty(prop, TypeFlags.StringOrNumberLiteralOrUnique), omitKeyType)
&& !(getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected))
&& isSpreadableProperty(prop)) {
members.set(prop.escapedName, getSpreadSymbol(prop));
}
}
return getTypeAliasInstantiation(restTypeAlias, [source, omitTypes]);
const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String);
const numberIndexInfo = getIndexInfoOfType(source, IndexKind.Number);
return createAnonymousType(symbol, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
}
/** Return the inferred type for a binding element */