有人可以给我一些建议.这里有一个对象数组:
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' },
];
Run Code Online (Sandbox Code Playgroud)
我想要做的是找到我如何在Typescript中定义它.
Rad*_*ler 13
不完全确定你的意思是:
我想要做的是找到我如何在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);
Run Code Online (Sandbox Code Playgroud)
在这里检查它
如果要查找避免使用接口的选项,则要声明该数组,以下两种语法有效:
contentOrderBy: { id: number, name: string, key: string }[];
Run Code Online (Sandbox Code Playgroud)
要么
contentOrderBy: Array<{ id: number, name: string, key: string }>;
Run Code Online (Sandbox Code Playgroud)
然后按照OP的问题填充数组。
由于我在寻找在对象内定义数组的正确方法时发现了这个问题,因此我还将添加该示例。在此示例中,对象的'key'属性是字符串数组。
contentOrderBy: { id: number, name: string, key: string[] }[];
Run Code Online (Sandbox Code Playgroud)
要么
contentOrderBy: Array<{ id: number, name: string, key: Array<string> }>;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23110 次 |
| 最近记录: |