我有以下界面和代码。我认为我做的定义是正确的,但我得到一个错误:

interface IenumServiceGetOrderBy { id: number; label: string; key: any }[];

and:

getOrderBy = (entity): IenumServiceGetOrderBy => {
        var result: IenumServiceGetOrderBy;
        switch (entity) {
            case "content":
                result =
                [
                    { 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'] }
                ];
                break;
        }
        return result;
    };

错误:

Error   190 Cannot convert '{}[]' to 'IenumServiceGetOrderBy':
    Type '{}[]' is missing property 'id' from type 'IenumServiceGetOrderBy'

当前回答

下面是一个内联版本,如果你不想创建一个全新的类型:

export interface ISomeInterface extends Array<{
    [someindex: string]: number;
}> { };

其他回答

简单的选项,没有tslint错误…

export interface MyItem {
    id: number
    name: string
}

export type MyItemList = [MyItem]

只要扩展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 }
 // OR
interface IenumServiceGetOrderBy { id: number; label: string; key: string | string[] }

// use interface like
const result: IenumServiceGetOrderBy[] = 
                [
                    { id: 0, label: 'CId', key: 'contentId' },
                    { id: 1, label: 'Modified By', key: 'modifiedBy' },
                    { id: 4, label: 'Status > Type', key: ['contentStatusId', 'contentTypeId'] }
                ];