Skip to content

Commit

Permalink
give suggestions when index signature given
Browse files Browse the repository at this point in the history
  • Loading branch information
collin5 committed Aug 14, 2018
1 parent fe387cc commit 24c5912
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/compiler/checker.ts
Expand Up @@ -9203,7 +9203,13 @@ namespace ts {
}
}
else {
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType));
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);
}
else {
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType));
}
}
}
}
Expand Down Expand Up @@ -18129,6 +18135,25 @@ 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) {
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 suggestions;
}

/**
* Given a name and a list of symbols whose names are *not* equal to the name, return a spelling suggestion if there is one that is close enough.
* Names less than length 3 only check for case-insensitive equality, not levenshtein distance.
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -3941,6 +3941,10 @@
"category": "Error",
"code": 7041
},
"Element implicitly has an 'any' type because type '{0}' has no index signature. Did you mean to call '{1}' ?": {
"category": "Error",
"code": 7042
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000
Expand Down

0 comments on commit 24c5912

Please sign in to comment.