谁能给我一些建议。我这里有一组对象:
contentOrderBy = [
{ id: 0, name: 'CId', key: 'contentId' },
{ id: 1, name: 'Modified By', key: 'modifiedBy' },
{ id: 2, name: 'Modified Date', key: 'modified' },
{ id: 3, name: 'Status', key: 'contentStatusId' },
];
我想做的是找到如何在 Typescript 中定义它。
最佳答案
不太清楚你的意思:
What I would like to do is to find how I can define this in Typescript.
但一种选择是引入接口(interface)
并将变量声明为此类对象的数组:
interface IMyEntity {
id: number;
name: string;
key: string;
}
var contentOrderBy = [
{ id: 0, name: 'CId', key: 'contentId' },
{ id: 1, name: 'Modified By', key: 'modifiedBy' },
{ id: 2, name: 'Modified Date', key: 'modified' },
{ id: 3, name: 'Status', key: 'contentStatusId' },
];
// finally here we have declaration of the array
// which contains the items of specified type/interface
// and we can later work with them "fully-typed"
var myContent : IMyEntity[] = contentOrderBy;
alert(myContent[0].name);
关于typescript - 如何使用 Typescript 定义对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937865/