-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Infer fixed length heterogenous arrays as tuples #13883
Comments
yeah, it would be nice. i just experienced the same issue. Typescript version: 2.2.x UNION TYPE INFERRED: function* CounterGenerator(): IterableIterator<number> {
let i = 1
while (1) {
yield i++
}
}
function* StringGenerator(): IterableIterator<string> {
let i = 1
while (1) {
yield String(i++)
}
}
function getTuple()/*: [number, string]*/ { // return type stripped
return [CounterGenerator().next().value, StringGenerator().next().value]
}
const [x, y] = getTuple()
console.log(x + parseInt(y)) // tsc complains about y NO ERRORS: function* CounterGenerator(): IterableIterator<number> {
let i = 1
while (1) {
yield i++
}
}
function* StringGenerator(): IterableIterator<string> {
let i = 1
while (1) {
yield String(i++)
}
}
function getTuple(): [number, string] {
return [CounterGenerator().next().value, StringGenerator().next().value]
}
const [x, y] = getTuple()
console.log(x + parseInt(y)) |
any updates on this? |
It is really unsafe to infer tuple types... There is a lot of discussion on this in other issues. It would and could break a lot of other code where someone is using an array that they don't intend to be a tuple. You have to choose an order of precedence of assign types when they overlap, and choosing an array is far more logical and common than a tuple type. Just because an array literal is a |
what is the use case of an heterogenous array that is not used as tuple? And it may be breaking, but I would assume only for a small amount of people and typescript had breaking changes before (2.1 -> 2.2). |
see also #15484 |
Duplicate of #12849 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
As the use of Heterogenous arrays for other purposes as tuples is minimal, I would change default inferance for fixed length heterogenous arrays to tuples to minimize the needed
as
statements in the code.TypeScript Version: 2.1
Expected behavior:
the tuple has the infered type
[string, number]
Actual behavior:
the tuple is typed
[string | number]
The text was updated successfully, but these errors were encountered: