TypeScript Version: 3.2.0
Code
This is the official demo
// Declare a tuple type
let x: [string, number];
// Actually锛宼his code will throw error
x[3] = 'world'; // OK, 'string' can be assigned to 'string | number'
Expected behavior:
I want to know if this is a bug or this is document error
Actual behavior:
Tuple out of bounds will trow error, but document say When accessing an element outside the set of known indices, a union type is used instead
Playground Link:
https://www.tslang.cn/play/index.html#src=let%20x%3A%20%5Bstring%2C%20number%5D%3B%0D%0A%2F%2F%20When%20accessing%20an%20element%20outside%20the%20set%20of%20known%20indices%2C%20a%20union%20type%20is%20used%20instead%3A%0D%0A%2F%2F%20Actually%EF%BC%8Cthis%20code%20will%20throw%20error%0D%0Ax%5B3%5D%20%3D%20%22world%22%3B%20%2F%2F%20OK%2C%20'string'%20can%20be%20assigned%20to%20'string%20%7C%20number'

Related Issues: NO
@weswigham
Tuples are considered fixed length - you may not assign to a numeric field outside it's range.
I guess it's a issue of https://github.com/Microsoft/TypeScript-Handbook. The quoted part is in https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Basic%20Types.md#tuple
Aye, the docs are out of date - I think tuples became fixed length around TS 2.5.
Thank you
Why there is nobody to fix this obvious misleading error on Doc?
Why there is nobody to fix this obvious misleading error on Doc?
Sadly, the project still employs a finite number of engineers.
Be the change you want to see in the world.
https://github.com/Microsoft/TypeScript-Handbook/pull/998
I've created this pr, hope can fix it.
why
should fix that obvious mistake
Most helpful comment
Aye, the docs are out of date - I think tuples became fixed length around TS 2.5.