Skip to content

Commit f8a035e

Browse files
committed
recommend set if el is on RHS of assignment else get
1 parent 623036b commit f8a035e

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

src/compiler/checker.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9285,9 +9285,9 @@ namespace ts {
92859285
}
92869286
}
92879287
else {
9288-
const suggestions = getSuggestionsForNonexistentIndexSignature(objectType);
9289-
if (suggestions) {
9290-
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);
9288+
const suggestion = getSuggestionForNonexistentIndexSignature(objectType, accessExpression);
9289+
if (suggestion) {
9290+
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);
92919291
}
92929292
else {
92939293
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType));
@@ -18455,23 +18455,32 @@ namespace ts {
1845518455
return suggestion && symbolName(suggestion);
1845618456
}
1845718457

18458-
function getSuggestionsForNonexistentIndexSignature(objectType: Type): string | undefined {
18459-
let suggestions: string | undefined;
18460-
const props = [
18461-
getPropertyOfObjectType(objectType, <__String>"get"),
18462-
getPropertyOfObjectType(objectType, <__String>"set")
18463-
];
18464-
18465-
for (const prop of props) {
18458+
function getSuggestionForNonexistentIndexSignature(objectType: Type, expr: ElementAccessExpression): string | undefined {
18459+
let suggestion: string | undefined;
18460+
// check if object type has setter or getter
18461+
const hasProp = (name: "set" | "get") => {
18462+
const prop = getPropertyOfObjectType(objectType, <__String>name);
1846618463
if (prop) {
1846718464
const s = getSingleCallSignature(getTypeOfSymbol(prop));
1846818465
if (s && getMinArgumentCount(s) === 1 && typeToString(getTypeAtPosition(s, 0)) === "string") {
18469-
const suggestion = symbolToString(objectType.symbol) + "." + symbolToString(prop);
18470-
suggestions = (!suggestions) ? suggestion : suggestions.concat(" or " + suggestion);
18466+
return true;
1847118467
}
1847218468
}
18469+
return false;
18470+
};
18471+
18472+
if (isAssignmentTarget(expr)) {
18473+
if (hasProp("set")) {
18474+
suggestion = symbolToString(objectType.symbol) + ".set";
18475+
}
1847318476
}
18474-
return suggestions;
18477+
else {
18478+
if (hasProp("get")) {
18479+
suggestion = symbolToString(objectType.symbol) + ".get";
18480+
}
18481+
}
18482+
18483+
return suggestion;
1847518484
}
1847618485

1847718486
/**

0 commit comments

Comments
 (0)