Skip to content

Commit

Permalink
recommend set if el is on RHS of assignment else get
Browse files Browse the repository at this point in the history
  • Loading branch information
collin5 committed Sep 11, 2018
1 parent 623036b commit f8a035e
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/compiler/checker.ts
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit f8a035e

Please sign in to comment.