如何使用 Typescript 定义对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31937865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I define an array of objects using Typescript?
提问by Radim K?hler
Can someone give me some advice. I have an array of objects here:
有人可以给我一些建议。我这里有一组对象:
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' },
];
What I would like to do is to find how I can define this in Typescript.
我想做的是找到如何在 Typescript 中定义它。
回答by Radim K?hler
Not fully sure what do you mean by:
不完全确定你的意思是什么:
What I would like to do is to find how I can define this in Typescript.
我想做的是找到如何在 Typescript 中定义它。
But one option is to introduce the interface
and declare variable as array of such objects:
但一种选择是引入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);
Check it in action here
回答by Julian Fraser
To declare the array the following two syntaxes are valid, if you are looking for an option that avoids the use of an interface:
要声明数组,以下两种语法是有效的,如果您正在寻找避免使用接口的选项:
contentOrderBy: { id: number, name: string, key: string }[];
or
或者
contentOrderBy: Array<{ id: number, name: string, key: string }>;
Then populate the array as in the OP's question.
然后按照 OP 的问题填充数组。
Since I found this question while searching for the correct way to define an array inside the object I will add that example also. In this example the 'key' property of the object is an array of strings.
由于我在寻找在对象内定义数组的正确方法时发现了这个问题,因此我还将添加该示例。在这个例子中,对象的“key”属性是一个字符串数组。
contentOrderBy: { id: number, name: string, key: string[] }[];
or
或者
contentOrderBy: Array<{ id: number, name: string, key: Array<string> }>;
回答by Rahul
You could try either of these. They are not giving me errors..
您可以尝试其中任何一个。他们没有给我错误..
//Declare with default value
private _possessions: Array<Thing> = new Array<Thing>();
or
或者
//declare
private _possessions: Array<Thing>;
constructor(){
//assign
this._possessions = new Array<Thing>();
}
回答by Umair Akbar
var storesArray : Store[] = [
{
app_category : "app_cat",
app_id : "11",
id : "12",
name : "g1",
target_audience: "tar_aud",
type: "intune"
},
{
app_category : "app_cat2",
app_id : "112",
id : "122",
name : "g12",
target_audience: "tar_aud2",
type: "intune2"
}]