From f8a035ea3e2861a10a06c85c39bc48db83ffc0ea Mon Sep 17 00:00:00 2001 From: Collins Abitekaniza Date: Wed, 12 Sep 2018 02:53:00 +0300 Subject: [PATCH] recommend set if el is on RHS of assignment else get --- src/compiler/checker.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f18fbedb80c08..64e65f0d51869 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9285,9 +9285,9 @@ namespace ts { } } else { - 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); + const suggestion = getSuggestionForNonexistentIndexSignature(objectType, accessExpression); + if (suggestion) { + error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1, typeToString(objectType), suggestion); } else { error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType)); @@ -18455,23 +18455,32 @@ 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) { + function getSuggestionForNonexistentIndexSignature(objectType: Type, expr: ElementAccessExpression): string | undefined { + let suggestion: string | undefined; + // check if object type has setter or getter + const hasProp = (name: "set" | "get") => { + const prop = getPropertyOfObjectType(objectType, <__String>name); 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 true; } } + return false; + }; + + if (isAssignmentTarget(expr)) { + if (hasProp("set")) { + suggestion = symbolToString(objectType.symbol) + ".set"; + } } - return suggestions; + else { + if (hasProp("get")) { + suggestion = symbolToString(objectType.symbol) + ".get"; + } + } + + return suggestion; } /**