如何在 TypeScript 中为这样的数组定义类型:
export const AlternativeSpatialReferences: Array< ??? > = [
{
'25833': 25833
},
{
'25832': 25832
},
{
'25831': 25831
},
{
'Google': 4326
}
];
现在我只使用 Array<{}>,但想要正确定义。
最佳答案
如果你想定义一个对象,其属性名称在编译时是未知的,而哪些值是数字,你应该使用“索引签名”(感谢@Joe Clay):
interface MyObject {
[propName: string]: number;
}
然后你可以这样写:
export const AlternativeSpatialReferences: MyObject[] = [
{
'25833': 25833
},
{
'25832': 25832
},
{
'25831': 25831
},
{
'Google': 4326
}
];
关于arrays - TypeScript 中带有对象的数组的类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829422/