Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create strongly typed array of arrays in TypeScript

Tags:

typescript

In a language like C# I can declare a list of lists like:

List<List<int>> list_of_lists; 

Is there a similar way to declare a strongly typed array of arrays in TypeScript? I tried the following approaches but neither compiles.

var list_of_lists:int[][]; var list_of_lists:Array<int[]>; 
like image 474
silentorb Avatar asked Apr 18 '14 20:04

silentorb


People also ask

How do I create a nested array in TypeScript?

To declare a two-dimensional array, set the type of the array as Type[][] , e.g. const arr: string[][] = [['one'], ['two']] . You can read the type as an array containing arrays of specific type.

Should I use [] or array in TypeScript?

There is no difference at all. Type[] is the shorthand syntax for an array of Type . Array<Type> is the generic syntax. They are completely equivalent.

How do you initialize an array of objects in TypeScript?

To declare an array of objects in TypeScript, set the type of the variable to {}[] , e.g. const arr: { name: string; age: number }[] = [] . Once the type is set, the array can only contain objects that conform to the specified type, otherwise the type checker throws an error. Copied!


1 Answers

int is not a type in TypeScript. You probably want to use number:

var listOfLists : number[][]; 
like image 140
Peter Olson Avatar answered Sep 22 '22 02:09

Peter Olson