@@ -10117,9 +10117,9 @@ namespace ts {
10117
10117
}
10118
10118
}
10119
10119
else {
10120
- const suggestions = getSuggestionsForNonexistentIndexSignature (objectType);
10121
- if (suggestions ) {
10122
- 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 );
10120
+ const suggestion = getSuggestionForNonexistentIndexSignature (objectType, accessExpression );
10121
+ if (suggestion !== undefined ) {
10122
+ 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 );
10123
10123
}
10124
10124
else {
10125
10125
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType));
@@ -20192,23 +20192,33 @@ namespace ts {
20192
20192
return suggestion && symbolName(suggestion);
20193
20193
}
20194
20194
20195
- function getSuggestionsForNonexistentIndexSignature(objectType: Type): string | undefined {
20196
- let suggestions: string | undefined;
20197
- const props = [
20198
- getPropertyOfObjectType(objectType, <__String>"get"),
20199
- getPropertyOfObjectType(objectType, <__String>"set")
20200
- ];
20201
-
20202
- for (const prop of props) {
20195
+ function getSuggestionForNonexistentIndexSignature(objectType: Type, expr: ElementAccessExpression): string | undefined {
20196
+ // check if object type has setter or getter
20197
+ const hasProp = (name: "set" | "get", argCount = 1) => {
20198
+ const prop = getPropertyOfObjectType(objectType, <__String>name);
20203
20199
if (prop) {
20204
20200
const s = getSingleCallSignature(getTypeOfSymbol(prop));
20205
- if (s && getMinArgumentCount(s) === 1 && typeToString(getTypeAtPosition(s, 0)) === "string") {
20206
- const suggestion = symbolToString(objectType.symbol) + "." + symbolToString(prop);
20207
- suggestions = (!suggestions) ? suggestion : suggestions.concat(" or " + suggestion);
20201
+ if (s && getMinArgumentCount(s) === argCount && typeToString(getTypeAtPosition(s, 0)) === "string") {
20202
+ return true;
20208
20203
}
20209
20204
}
20205
+ return false;
20206
+ };
20207
+
20208
+ const suggestedMethod = isAssignmentTarget(expr) ? "set" : "get";
20209
+ if (!hasProp(suggestedMethod)) {
20210
+ return undefined;
20211
+ }
20212
+
20213
+ let suggestion = tryGetPropertyAccessOrIdentifierToString(expr);
20214
+ if (suggestion === undefined) {
20215
+ suggestion = suggestedMethod;
20210
20216
}
20211
- return suggestions;
20217
+ else {
20218
+ suggestion += "." + suggestedMethod;
20219
+ }
20220
+
20221
+ return suggestion;
20212
20222
}
20213
20223
20214
20224
/**
0 commit comments