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[]>;
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.
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.
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!
int
is not a type in TypeScript. You probably want to use number
:
var listOfLists : number[][];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With