Skip to content

Tuples are inferred as arrays, losing information #13151

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

Closed
pelotom opened this issue Dec 23, 2016 · 3 comments
Closed

Tuples are inferred as arrays, losing information #13151

pelotom opened this issue Dec 23, 2016 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@pelotom
Copy link

pelotom commented Dec 23, 2016

TypeScript Version: 2.1.4

The inferred type of a tuple expression is an array, for example

const foo = ['hello', 3, true] // foo: (string | number | boolean)[]

which is a pain when what you really wanted was a tuple; you need to give a type ascription in order to tell the compiler you meant it to be a tuple, e.g.

const foo: [string, number, boolean] = ['hello', 3, true]

It seems like this is wholly unnecessary, because the type [string, number, boolean] is assignable to (string | number | boolean)[], and carries strictly more information. So the compiler is prematurely throwing away information, when it could just wait and see if later on the type needs to be specialized to an array.

@normalser
Copy link

FYI #11924 (comment)

@pelotom
Copy link
Author

pelotom commented Dec 23, 2016

Aha, mutability rears its ugly head once again :(

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 26, 2016
@zpdDG4gta8XKpMCd
Copy link

There is a thread on adding special syntax for literals, including tuples: #10195

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants