镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

check index access for fixed length tuple - #26292

Merged
Ryan Cavanaugh (RyanCavanaugh) merged 1 commit into
microsoft:masterfrom
Kingwl:tupleIndexAccessCheck
Sep 5, 2018
Merged

Ryan Cavanaugh (RyanCavanaugh) merged 1 commit into
microsoft:masterfrom
Kingwl:tupleIndexAccessCheck

Conversation

@Kingwl

Copy link
Copy Markdown
Contributor

Fixes #5203

Comment thread src/compiler/checker.ts
}
if (isTupleType(objectType) && !objectType.target.hasRestElement && isNumericLiteral(indexExpression)) {
const index = +indexExpression.text;
const maximumIndex = length(objectType.target.typeParameters);

@IllusionMH Andrii Dieiev (IllusionMH) Aug 9, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, my fault

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like empty tuples will require separate handling & error message to avoid the maximum of index is '-1'.

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 2732
},
"Indexed access '{0}' is out of range of tuple, the maximum of index is '{1}'.": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposed phrasing

Index '{0}' is out-of-bounds in tuple of length {1}

cc Daniel Rosenwasser (@DanielRosenwasser)

Comment thread src/compiler/checker.ts Outdated
}
if (isTupleType(objectType) && !objectType.target.hasRestElement && isNumericLiteral(indexExpression)) {
const index = +indexExpression.text;
const maximumIndex = length(objectType.target.typeParameters) - 1;

@IllusionMH Andrii Dieiev (IllusionMH) Aug 10, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I had to remove my comments as soon as Ryan Cavanaugh (@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.

@RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) merged commit 6465e9d into microsoft:master Sep 5, 2018
@Kingwl
Wenlu Wang (Kingwl) deleted the tupleIndexAccessCheck branch September 5, 2018 23:47
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants