-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tuple allows access at any index #10965
Comments
This works because indexing is not verified. You can always index. What is weird is that |
The current behaviour is to give 'unknown' elements the union of the tuple element types. It's clearer to see if the tuple has different element types: const cell: [ string, number ] = [ '0', 1 ];
const u = cell[0];
u // u is string
const v = cell[1];
u // u is number
const y = cell[100]
y // y is string | number But personally I don't think that's a logical deduction for the type system to make. All bets are off for indexes outside the tuple range. At runtime they are likely to return |
@yortus what I find odd is this const cell: [ string, number ] = [ '0', 1 ];
const u = cell['0'];
u // u is string
const v = cell['1'];
u // u is number
const y = cell[100]
y // y is string | number
const y = cell['100']
y // y is any |
See a proposal of fixing this #6229 |
Duplicate #5203 |
I noticed this as well, sort of related. Can anyone explain why? const xs = [1,2,3];
const x5 = xs[5]; // type is number, expected number | undefined |
Version: 2 rc
Given:
I expect
cell[100]
to error, but it doesn't.y
has a type of number. This means it's very easy to me to have bugs whereby I'm asking for an index that doesn't exist (like asking for a property that doesn't exist on an object).The text was updated successfully, but these errors were encountered: