Skip to content

What it type [[string, number]] vs [string, number][] #3428

Closed

Description

@unional
var items: [[string, number]];
for (let item of items) { // item: [string, number]
   let name = item[0];  // name: string
   let quantity = item[1]; // quantity: number
}
var items: [string, number][];
for (let item of items) { // item: [string, number]
   let name = item[0];  // name: string
   let quantity = item[1]; // quantity: number
}

They look the same but can't assign to each other:

var a: [[string, number]];
var b: [string, number][];
a = b.map(c => c); // Error: Type '[string, number][]' is not assignable to type '[[string, number]]'.  Property '0' is missing in type '[string, number][]'
a = b.map(c => [c[0], c[1]]); // Error: Type '(string | number)[][]' is not assignable to type '[[string, number]]'. Property '0' is missing in type '(string | number)[][]'
b = a.map(c => c); // OK
b = a.map(c => [c[0], c[1]]); // Error: Type '(string | number)[][]' is not assignable to type '[[string, number]]'. Type '(string | number)[]' is not assignable to type '[string, number]'. Property '0' is missing in type '(string | number)[]'

Activity

  1. DanielRosenwasser commented on Jun 8, 2015

    @DanielRosenwasser
    Member

    [[string, number]] is a single-element tuple where the element has the type [string, number].

    [string, number][] is an array of [string, number] tuples.

    Homa Wong (@unional), I think questions such as these would be better fit for either StackOverflow or our Gitter room.

  2. unional commented on Jun 8, 2015

    @unional
    ContributorAuthor

    Thanks. Didn't know gitter room exist. Will move questions like these over there.

  3. DanielRosenwasser commented on Jun 8, 2015

    @DanielRosenwasser
    Member

    No worries - we only recently got the Gitter room so it's easy to miss.

  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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions