You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Infer fixed length heterogenous arrays as tuples #13883
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
constf : (s : string)=>number=s=>parseInt(s);constarr : string[]=['0','1','2'];constres=arr.map(s=>[s,f(s)])//Should be infered as [string, number] not [string | number].map(([s,n])=>f(s)+n);//Error: s has type number | string
Expected behavior:
the tuple has the infered type [string, number]
Actual behavior:
the tuple is typed [string | number]
yeah, it would be nice. i just experienced the same issue.
Typescript version: 2.2.x
UNION TYPE INFERRED:
function*CounterGenerator(): IterableIterator<number>{leti=1while(1){yieldi++}}function*StringGenerator(): IterableIterator<string>{leti=1while(1){yieldString(i++)}}functiongetTuple()/*: [number, string]*/{// return type strippedreturn[CounterGenerator().next().value,StringGenerator().next().value]}const[x,y]=getTuple()console.log(x+parseInt(y))// tsc complains about y
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 const and is heterogeneous doesn't mean that it is intended to be a tuple. Especially because even with tuple types, all the run-time array functionality is available. It makes a lot of sense that tuples need to be explicitly declared as it demonstrates the intent of ambiguous code.
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).
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
phra commentedon Apr 3, 2017
yeah, it would be nice. i just experienced the same issue.
Typescript version: 2.2.x
UNION TYPE INFERRED:
NO ERRORS:
phra commentedon Apr 30, 2017
any updates on this?
kitsonk commentedon May 1, 2017
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
constand is heterogeneous doesn't mean that it is intended to be a tuple. Especially because even with tuple types, all the run-time array functionality is available. It makes a lot of sense that tuples need to be explicitly declared as it demonstrates the intent of ambiguous code.jvanbruegge commentedon May 1, 2017
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).
phra commentedon May 1, 2017
see also #15484
mhegazy commentedon Feb 8, 2018
Duplicate of #12849
typescript-bot commentedon Feb 22, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.