pod*_*eig 3 arrays typescript typescript-typings
如何在 TypeScript 中为这样的数组定义类型:
export const AlternativeSpatialReferences: Array< ??? > = [
{
'25833': 25833
},
{
'25832': 25832
},
{
'25831': 25831
},
{
'Google': 4326
}
];
Run Code Online (Sandbox Code Playgroud)
现在我只使用 Array<{}>,但想要正确定义。
如果要定义一个对象,其属性名称在编译时未知,哪些值为数字,则应使用“索引签名”(感谢@Joe Clay):
interface MyObject {
[propName: string]: number;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以写:
export const AlternativeSpatialReferences: MyObject[] = [
{
'25833': 25833
},
{
'25832': 25832
},
{
'25831': 25831
},
{
'Google': 4326
}
];
Run Code Online (Sandbox Code Playgroud)
在打字稿中你使用anytype ,
any 用于 - 需要描述我们在编写应用程序时不知道的变量类型。
Array<any>
Run Code Online (Sandbox Code Playgroud)
如果您想要某种强类型,那么您应该创建具有两个属性的新类
public class KeyValue
{
key:string;
value:number;
}
let myarray: KeyValue[] = new Array<KeyValue>();
myarray.push({key: '25833' , value : 25833});
myarray.push({key: 'Google' , value : 123});
Run Code Online (Sandbox Code Playgroud)
并将当前数组值转换为强类型。
| 归档时间: |
|
| 查看次数: |
11796 次 |
| 最近记录: |