From 856f754fad416da003b76b4116b78e5e51a61943 Mon Sep 17 00:00:00 2001 From: Collins Abitekaniza Date: Tue, 14 Aug 2018 15:09:56 +0300 Subject: [PATCH] give suggestions when index signature given --- src/compiler/checker.ts | 27 ++++++++++++++++++++++++++- src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 829d11bd09173..af9c5a5f4a0cc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9187,7 +9187,13 @@ namespace ts { } } else { - error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType)); + const suggestions = getSuggestionsForNonexistentIndexSignature(objectType); + if (suggestions) { + error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1, typeToString(objectType), suggestions); + } + else { + error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType)); + } } } } @@ -18159,6 +18165,25 @@ namespace ts { return suggestion && symbolName(suggestion); } + function getSuggestionsForNonexistentIndexSignature(objectType: Type): string | undefined { + let suggestions: string | undefined; + const props = [ + getPropertyOfObjectType(objectType, <__String>"get"), + getPropertyOfObjectType(objectType, <__String>"set") + ]; + + for (const prop of props) { + if (prop) { + const s = getSingleCallSignature(getTypeOfSymbol(prop)); + if (s && getMinArgumentCount(s) === 1 && typeToString(getTypeAtPosition(s, 0)) === "string") { + const suggestion = symbolToString(objectType.symbol) + "." + symbolToString(prop); + suggestions = (!suggestions) ? suggestion : suggestions.concat(" or " + suggestion); + } + } + } + return suggestions; + } + /** * Given a name and a list of symbols whose names are *not* equal to the name, return a spelling suggestion if there is one that is close enough. * Names less than length 3 only check for case-insensitive equality, not levenshtein distance. diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 6000c48de4dc3..7379a649a3efe 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3945,6 +3945,10 @@ "category": "Error", "code": 7042 }, + "Element implicitly has an 'any' type because type '{0}' has no index signature. Did you mean to call '{1}' ?": { + "category": "Error", + "code": 7043 + }, "You cannot rename this element.": { "category": "Error", "code": 8000