Merge pull request #31687 from microsoft/enumImplicitIndexSignatures

Implicit index signatures for enum object types
This commit is contained in:
Anders Hejlsberg
2019-06-02 07:42:58 -07:00
committed by GitHub
6 changed files with 165 additions and 6 deletions

View File

@@ -7316,7 +7316,8 @@ namespace ts {
stringIndexInfo = createIndexInfo(anyType, /*isReadonly*/ false);
}
}
const numberIndexInfo = symbol.flags & SymbolFlags.Enum ? enumNumberIndexInfo : undefined;
const numberIndexInfo = symbol.flags & SymbolFlags.Enum && (getDeclaredTypeOfSymbol(symbol).flags & TypeFlags.Enum ||
some(type.properties, prop => !!(getTypeOfSymbol(prop).flags & TypeFlags.NumberLike))) ? enumNumberIndexInfo : undefined;
setStructuredTypeMembers(type, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
// We resolve the members before computing the signatures because a signature may use
// typeof with a qualified name expression that circularly references the type we are
@@ -8194,6 +8195,9 @@ namespace ts {
propTypes.push(getTypeOfSymbol(prop));
}
}
if (kind === IndexKind.String) {
append(propTypes, getIndexTypeOfType(type, IndexKind.Number));
}
if (propTypes.length) {
return getUnionType(propTypes, UnionReduction.Subtype);
}
@@ -14615,7 +14619,7 @@ namespace ts {
* with no call or construct signatures.
*/
function isObjectTypeWithInferableIndex(type: Type) {
return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.ValueModule)) !== 0 &&
return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.Enum | SymbolFlags.ValueModule)) !== 0 &&
!typeHasCallOrConstructSignatures(type);
}