1
我测试打字稿,我跑了与数组,接口和可空变量的烦恼:打字稿可空数组
我有这样的接口:
interface Entity {
life: number;
type: EntityType;
}
而且我有这样的特性:
world: Entity[];
我试图用以下方式进行初始化:
this.world = [
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null]
];
编译器告诉我:
无法转换{} []以实体[]
这可能吗?难道我做错了什么。
在此先感谢!
PS:对不起,我不是母语的人。
是的,我明白了为什么编译器很生气。感谢演员的语法。我不知道。感谢2D Array语法。我不知道这种语法在Typescript中是可能的。 – fharreau
+1。这在TypeScript中被称为“类型断言”。 – Fenton
谢谢史蒂夫,赶上。 –