Typescript: out of range index in a tuple magically works

Created on 10 Oct 2015  路  9Comments  路  Source: microsoft/TypeScript


var x : [number, number] = [1, 2];
var y = x[2]; // expected a compile error, actual ok

By design, isn't it?

Committed Suggestion good first issue help wanted

Most helpful comment

god works in mysterious ways

All 9 comments

It is currently per spec, but I don't think this behavior is desirable. This should probably be an error by some new rule.

Well the problem is that you're going to get that numeric index signature from Array<T>. There is another discussion about having an alternative interface for tuples at https://github.com/Microsoft/TypeScript/issues/4988#issuecomment-145705203.

I know guys you are trying your best, but I don't see an array, I only see a tuple which is not an array in a broad sense.

:+1: This is invalid behavior.

I ran into exactly the same issue.

See proposal #6229

It's now an error for tuples to have extra elements (#17765), so maybe we could revisit this? @sandersn @RyanCavanaugh

@aleksey-bykov the arc of language design is long but it bends toward justice. Accepting PRs

god works in mysterious ways

Was this page helpful?
0 / 5 - 0 ratings