Add readonly check to property access of index signature

Unfortunately, the checking code isn't reusable in a large chunk, so I copied it
from `getPropertyTypeForIndexType`. It still might be better to extract
the four lines to report the error into its own function.
This commit is contained in:
Nathan Shively-Sanders 2017-08-09 16:14:00 -07:00
parent e1ba65ae64
commit 50b2b77f44

View file

@ -14599,9 +14599,13 @@ namespace ts {
}
const prop = getPropertyOfType(apparentType, right.escapedText);
if (!prop) {
const stringIndexType = getIndexTypeOfType(apparentType, IndexKind.String);
if (stringIndexType) {
return stringIndexType;
const indexInfo = getIndexInfoOfType(apparentType, IndexKind.String);
if (indexInfo && indexInfo.type) {
if (indexInfo.isReadonly && (isAssignmentTarget(node) || isDeleteTarget(node))) {
error(node, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(apparentType));
return unknownType;
}
return indexInfo.type;
}
if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) {
reportNonexistentProperty(right, type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType ? apparentType : type);