From 50b2b77f440788f8023488d5fdbeb0c1d0a3c661 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 9 Aug 2017 16:14:00 -0700 Subject: [PATCH] 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. --- src/compiler/checker.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 46cb8bb4937..5b1d4d77bf7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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);