Skip to content

Tuples are inferred as arrays, losing information #13151

Closed

Description

@pelotom

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.

Activity

  1. normalser commented on Dec 23, 2016

    @normalser
  2. pelotom commented on Dec 23, 2016

    @pelotom
    Author

    Aha, mutability rears its ugly head once again :(

  3. zpdDG4gta8XKpMCd commented on Dec 27, 2016

    @zpdDG4gta8XKpMCd

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

  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions