Tob*_*ngl 5 typescript ecmascript-6 definitelytyped typescript-typings
我有一个这样的代码片段:
export class TagCloud {
tags: [Tag];
locations: [Location];
constructor() {
this.tags = new Array<Tag>();
this.locations = new Array<Location>();
}
}
Run Code Online (Sandbox Code Playgroud)
但这给了我以下错误:
错误TS2322:类型'Tag []'不能分配给'[Tag]'类型.'Tag []'类型中缺少属性'0'.
错误TS2322:类型'位置[]'不能分配给'[Lo cation]'类型."Location []"类型中缺少属性"0".
我做错了什么(代码工作正常)?
我正在使用带有es6-shim类型描述的打字符(https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/es6-shim).
Nit*_*mer 11
在声明数组的typescript中,您可以执行以下操作:
let a: Array<number>;
Run Code Online (Sandbox Code Playgroud)
要么
let a: number[];
Run Code Online (Sandbox Code Playgroud)
当你使用:
let a: [number];
Run Code Online (Sandbox Code Playgroud)
你实际上是在声明一个元组,在这种情况下长度为1.
这是另一个元组:
let a: [number, string, string];
Run Code Online (Sandbox Code Playgroud)
你得到这个错误的原因是因为数组的长度分配给tags和locations为0,它应该是1.
| 归档时间: |
|
| 查看次数: |
23926 次 |
| 最近记录: |