Skip to content

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

@unional

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)[]'

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