在 TypeScript 中创建强类型数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23161486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create strongly typed array of arrays in TypeScript
提问by silentorb
In a language like C# I can declare a list of lists like:
在像 C# 这样的语言中,我可以声明一个列表列表,例如:
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.
是否有类似的方法在 TypeScript 中声明强类型数组?我尝试了以下方法,但都没有编译。
var list_of_lists:int[][];
var list_of_lists:Array<int[]>;
回答by Peter Olson
int
is not a type in TypeScript. You probably want to use number
:
int
不是 TypeScript 中的类型。你可能想使用number
:
var listOfLists : number[][];
回答by Damian Wojakowski
You do it exactly like in Java/C#, e.g. (in a class):
你完全像在 Java/C# 中那样做,例如(在一个类中):
class SomeClass {
public someVariable: Array<Array<AnyTypeYouWant>>;
}
Or as a standalone variable:
或作为独立变量:
var someOtherVariable: Array<Array<AnyTypeYouWant>>;
回答by James Harris
@Eugene you can use something like Array<Array<[string, number]>>
to define a Map type
@Eugene 您可以使用类似的东西Array<Array<[string, number]>>
来定义 Map 类型