Skip to content

Commit 6763389

Browse files
committed
Improve error message for out-of-bounds tuple element access
1 parent 54b46d7 commit 6763389

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9609,7 +9609,13 @@ namespace ts {
96099609
if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
96109610
if (accessNode && everyType(objectType, t => !(<TupleTypeReference>t).target.hasRestElement)) {
96119611
const indexNode = getIndexNodeForAccessExpression(accessNode);
9612-
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
9612+
if (isTupleType(objectType)) {
9613+
error(indexNode, Diagnostics.Tuple_type_0_of_length_1_has_no_element_at_index_2,
9614+
typeToString(objectType), getTypeReferenceArity(objectType), unescapeLeadingUnderscores(propName));
9615+
}
9616+
else {
9617+
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
9618+
}
96139619
}
96149620
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
96159621
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,10 @@
17521752
"category": "Error",
17531753
"code": 2492
17541754
},
1755+
"Tuple type '{0}' of length '{1}' has no element at index '{2}'.": {
1756+
"category": "Error",
1757+
"code": 2493
1758+
},
17551759
"Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher.": {
17561760
"category": "Error",
17571761
"code": 2494

0 commit comments

Comments
 (0)