Improve error message for out-of-bounds tuple element access

This commit is contained in:
Anders Hejlsberg 2018-12-19 08:45:18 -08:00
parent 54b46d74b5
commit 676338971d
2 changed files with 11 additions and 1 deletions

View File

@ -9609,7 +9609,13 @@ namespace ts {
if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
if (accessNode && everyType(objectType, t => !(<TupleTypeReference>t).target.hasRestElement)) {
const indexNode = getIndexNodeForAccessExpression(accessNode);
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
if (isTupleType(objectType)) {
error(indexNode, Diagnostics.Tuple_type_0_of_length_1_has_no_element_at_index_2,
typeToString(objectType), getTypeReferenceArity(objectType), unescapeLeadingUnderscores(propName));
}
else {
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
}
}
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
}

View File

@ -1752,6 +1752,10 @@
"category": "Error",
"code": 2492
},
"Tuple type '{0}' of length '{1}' has no element at index '{2}'.": {
"category": "Error",
"code": 2493
},
"Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher.": {
"category": "Error",
"code": 2494