Try to provide a spelling suggestion when object literals have excess properties.

This commit is contained in:
Daniel Rosenwasser
2017-08-18 12:55:35 -07:00
parent 70ad2bdb31
commit 50671c374c
2 changed files with 22 additions and 3 deletions
+18 -3
View File
@@ -9067,11 +9067,26 @@ namespace ts {
else {
// use the property's value declaration if the property is assigned inside the literal itself
const objectLiteralDeclaration = source.symbol && firstOrUndefined(source.symbol.declarations);
let suggestion;
if (prop.valueDeclaration && findAncestor(prop.valueDeclaration, d => d === objectLiteralDeclaration)) {
errorNode = prop.valueDeclaration;
const propDeclaration = prop.valueDeclaration as ObjectLiteralElementLike;
Debug.assertNode(propDeclaration, isObjectLiteralElementLike);
errorNode = propDeclaration;
const propNameNode = propDeclaration.name as Identifier;
Debug.assertNode(propNameNode, isIdentifier);
suggestion = getSuggestionForNonexistentProperty(propNameNode, target);
}
if (suggestion !== undefined) {
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_but_0_does_not_exist_in_type_1_Did_you_mean_to_write_2,
symbolToString(prop), typeToString(target), unescapeLeadingUnderscores(suggestion));
}
else {
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
symbolToString(prop), typeToString(target));
}
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
symbolToString(prop), typeToString(target));
}
}
return true;
+4
View File
@@ -1912,6 +1912,10 @@
"category": "Error",
"code": 2560
},
"Object literal may only specify known properties, but '{0}' does not exist in type '{1}'. Did you mean to write '{2}'?": {
"category": "Error",
"code": 2561
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600