Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
give suggestions when index signature given
  • Loading branch information
collin5 committed Apr 30, 2019
commit e49b841b4d74b390df0bd3a7471f1cc5913d9d89
27 changes: 26 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10117,7 +10117,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 @@ -20186,6 +20192,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
5 changes: 4 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4272,7 +4272,10 @@
"category": "Error",
"code": 7051
},

"Element implicitly has an 'any' type because type '{0}' has no index signature. Did you mean to call '{1}' ?": {
Copy link
Copy Markdown
Member

@DanielRosenwasser DanielRosenwasser Aug 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the stray space at the end.

"category": "Error",
"code": 7052
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000
Expand Down