Typescript: is out-of-bounds in tuple of length锛宐ut the introduction in the document can be out of bounds

Created on 7 Dec 2018  路  10Comments  路  Source: microsoft/TypeScript


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'

image

Related Issues: NO

Docs

Most helpful comment

Aye, the docs are out of date - I think tuples became fixed length around TS 2.5.

All 10 comments

@weswigham

Tuples are considered fixed length - you may not assign to a numeric field outside it's range.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

weswigham picture weswigham  路  3Comments

siddjain picture siddjain  路  3Comments