check index access for fixed length tuple#26292
Conversation
7c9d1d4 to
3421531
Compare
| } | ||
| if (isTupleType(objectType) && !objectType.target.hasRestElement && isNumericLiteral(indexExpression)) { | ||
| const index = +indexExpression.text; | ||
| const maximumIndex = length(objectType.target.typeParameters); |
There was a problem hiding this comment.
No longer needed after message change.
Looks like there should be length(...) - 1 to get last available index (or add - 1 to error param). Otherwise errors looks off by 1 and confusing.
P.S. Thank you for PR. Glad to see that this will be in TS soon. 👍
| let x = [] as []; | ||
| let y = x[0]; | ||
| ~ | ||
| !!! error TS2733: Indexed access '0' is out of range of tuple, the maximum of index is '0'. No newline at end of file |
There was a problem hiding this comment.
Looks like empty tuples will require separate handling & error message to avoid the maximum of index is '-1'.
| "category": "Error", | ||
| "code": 2732 | ||
| }, | ||
| "Indexed access '{0}' is out of range of tuple, the maximum of index is '{1}'.": { |
There was a problem hiding this comment.
3421531 to
e90f645
Compare
| } | ||
| if (isTupleType(objectType) && !objectType.target.hasRestElement && isNumericLiteral(indexExpression)) { | ||
| const index = +indexExpression.text; | ||
| const maximumIndex = length(objectType.target.typeParameters) - 1; |
There was a problem hiding this comment.
Sorry, I had to remove my comments as soon as @RyanCavanaugh proposed to just change error message text (which I think better solution - it handled cases with empty tuples well without special treatment).
With new message - changes that I proposed earlier only add off by one error in mesage 😞 and should be reverted.
e90f645 to
6432bd9
Compare
Fixes #5203