Skip to content

Commit 2e5a39a

Browse files
committed
Error when indexing out of bounds in a tuple or union of tuples
1 parent 62aeead commit 2e5a39a

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9312,7 +9312,12 @@ namespace ts {
93129312
propType;
93139313
}
93149314
if (everyType(objectType, isTupleType) && isNumericLiteralName(propName) && +propName >= 0) {
9315-
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
9315+
const restType = mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
9316+
if (restType === undefinedType && accessNode) {
9317+
const indexNode = accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode.argumentExpression : accessNode.indexType;
9318+
error(indexNode, Diagnostics.Property_0_does_not_exist_on_type_1, unescapeLeadingUnderscores(propName), typeToString(objectType));
9319+
}
9320+
return restType;
93169321
}
93179322
}
93189323
if (!(indexType.flags & TypeFlags.Nullable) && isTypeAssignableToKind(indexType, TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.ESSymbolLike)) {
@@ -18964,13 +18969,6 @@ namespace ts {
1896418969
error(indexExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
1896518970
return errorType;
1896618971
}
18967-
if (isTupleType(objectType) && !objectType.target.hasRestElement && isNumericLiteral(indexExpression)) {
18968-
const index = +indexExpression.text;
18969-
const length = getTypeReferenceArity(objectType);
18970-
if (index >= length) {
18971-
error(indexExpression, Diagnostics.Index_0_is_out_of_bounds_in_tuple_of_length_1, index, length);
18972-
}
18973-
}
1897418972

1897518973
return checkIndexedAccessIndexType(getIndexedAccessType(objectType, isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType, node), node);
1897618974
}

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,10 +2481,6 @@
24812481
"category": "Error",
24822482
"code": 2732
24832483
},
2484-
"Index '{0}' is out-of-bounds in tuple of length {1}.": {
2485-
"category": "Error",
2486-
"code": 2733
2487-
},
24882484
"It is highly likely that you are missing a semicolon.": {
24892485
"category": "Error",
24902486
"code": 2734

0 commit comments

Comments
 (0)