From 8f932f9597da19e59f886c21ee5049afb58881b1 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 2cb66d2d3417b..6e16e0424c53d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9476,9 +9476,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)); @@ -18790,23 +18790,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; } /**