Можно определить интерфейс с индексатор :
interface EnumServiceGetOrderBy {
[index: number]: { id: number; label: string; key: any };
}
Можно определить интерфейс как массив с простым расширением интерфейса Array.
export interface MyInterface extends Array<MyType> { }
С этим, любой объект, который реализует эти MyInterface
, должен будет реализовать все вызовы функции массивов и только сможет хранить объекты с эти MyType
тип.
Дополнительная легкая опция:
interface simpleInt {
id: number;
label: string;
key: any;
};
type simpleType = simpleInt[];
Вот встроенная версия, если Вы не хотите создавать совершенно новый тип:
export interface ISomeInterface extends Array<{
[someindex: string]: number;
}> { };
Также можно сделать это.
interface IenumServiceGetOrderBy {
id: number;
label: string;
key: any;
}
// notice i am not using the []
var oneResult: IenumServiceGetOrderBy = { id: 0, label: 'CId', key: 'contentId'};
//notice i am using []
// it is read like "array of IenumServiceGetOrderBy"
var ArrayOfResult: IenumServiceGetOrderBy[] =
[
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 2, label: 'Modified Date', key: 'modified' },
{ id: 3, label: 'Status', key: 'contentStatusId' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
{ id: 5, label: 'Title', key: 'title' },
{ id: 6, label: 'Type', key: 'contentTypeId' },
{ id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
];
В Угловом использовании 'расширяется' для определения интерфейса для 'Массива' объектов.
Используя индексатор даст Вам ошибку как не, интерфейс Array так не содержит свойства и методы.
, например,
ошибка TS2339: Свойство 'находит', не существует на типе 'ISelectOptions2'.
// good
export interface ISelectOptions1 extends Array<ISelectOption> {}
// bad
export interface ISelectOptions2 {
[index: number]: ISelectOption;
}
interface ISelectOption {
prop1: string;
prop2?: boolean;
}
Легкая опция без tslint ошибок...
export interface MyItem {
id: number
name: string
}
export type MyItemList = [MyItem]
Вы не делаете потребность для использования индексатора (начиная с него немного менее безопасный с точки зрения типов). У Вас есть две опции:
interface EnumServiceItem {
id: number; label: string; key: any
}
interface EnumServiceItems extends Array<EnumServiceItem>{}
// Option A
var result: EnumServiceItem[] = [
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 2, label: 'Modified Date', key: 'modified' },
{ id: 3, label: 'Status', key: 'contentStatusId' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
{ id: 5, label: 'Title', key: 'title' },
{ id: 6, label: 'Type', key: 'contentTypeId' },
{ id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
];
// Option B
var result: EnumServiceItems = [
{ id: 0, label: 'CId', key: 'contentId' },
{ id: 1, label: 'Modified By', key: 'modifiedBy' },
{ id: 2, label: 'Modified Date', key: 'modified' },
{ id: 3, label: 'Status', key: 'contentStatusId' },
{ id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] },
{ id: 5, label: 'Title', key: 'title' },
{ id: 6, label: 'Type', key: 'contentTypeId' },
{ id: 7, label: 'Type > Status', key: ['contentTypeId', 'contentStatusId'] }
]
Лично я рекомендую Опцию A (более простая миграция, когда Вы используете классы не интерфейсы).