Add index signature for anonymous object literal type (#37903)

* Use ts.map for stylistic consistency

* Show error only if noImplicitAny is set

* Accept baseline for noImplicitAnyIndexing

* Fix lint error

* Add test cases for indexedAccessWithFreshObjectLiteral
This commit is contained in:
okmttdhr
2020-11-03 07:35:56 +09:00
committed by GitHub
parent f646ec87fc
commit 7db5f68144
12 changed files with 608 additions and 32 deletions

View File

@@ -13878,6 +13878,19 @@ namespace ts {
return anyType;
}
if (accessExpression && !isConstEnumObjectType(objectType)) {
if (isObjectLiteralType(objectType)) {
if (noImplicitAny && indexType.flags & (TypeFlags.StringLiteral | TypeFlags.NumberLiteral)) {
diagnostics.add(createDiagnosticForNode(accessExpression, Diagnostics.Property_0_does_not_exist_on_type_1, (indexType as StringLiteralType).value, typeToString(objectType)));
return undefinedType;
}
else if (indexType.flags & (TypeFlags.Number | TypeFlags.String)) {
const types = map((<ResolvedType>objectType).properties, property => {
return getTypeOfSymbol(property);
});
return getUnionType(append(types, undefinedType));
}
}
if (objectType.symbol === globalThisSymbol && propName !== undefined && globalThisSymbol.exports!.has(propName) && (globalThisSymbol.exports!.get(propName)!.flags & SymbolFlags.BlockScoped)) {
error(accessExpression, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
}