arrays - TypeScript 中带有对象的数组的类型定义

标签 arrays typescript typescript-typings

如何在 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/

相关文章:

typescript - 如何定义字符串类型 |数字?

TypeScript:定义了解子类属性的父类构造函数

reactjs - 类型 'ref' 上不存在属性 'IntrinsicAttributes'

c - 如何返回字符数组?

python - 通过两个函数运行循环,将彼此的输出作为输入

javascript - 自动重定向到扫描的链接

javascript - 访问 json 对象中的元素

typescript - 如何制作依赖于先前值的 typescript 定义?

javascript - MD 数组 : Add additional array if value isn't found

ASP.NET MVC-如何将数组传递给 View ?