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

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'

当前回答

所以我要补充我的意见:)

假设您需要一个充满person的Workplace类型。 你需要这样的东西:

错了!... 作为接口


interface Workplace {
   name: string
   age: number
}[] 
// that [] doesn't work!! ;)

这里的问题是接口是用于指定类/对象形状..仅此而已。所以你可以用type来代替,它更灵活

更好->使用类型而不是接口


type Workplace = {
   name: string
   age: number
}[] 

// This works :D

但也许最好的是……

将内部对象Person定义为interface,然后Workplace定义为Person数组生成的类型-> Person[]


interface Person {
   name: string
   age: number
}

type Workplace = Person[]

// nice ;)

祝你好运

其他回答

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

export interface MyItem {
    id: number
    name: string
}

export type MyItemList = [MyItem]

你也可以这样做。

            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'] }
            ];

所以我要补充我的意见:)

假设您需要一个充满person的Workplace类型。 你需要这样的东西:

错了!... 作为接口


interface Workplace {
   name: string
   age: number
}[] 
// that [] doesn't work!! ;)

这里的问题是接口是用于指定类/对象形状..仅此而已。所以你可以用type来代替,它更灵活

更好->使用类型而不是接口


type Workplace = {
   name: string
   age: number
}[] 

// This works :D

但也许最好的是……

将内部对象Person定义为interface,然后Workplace定义为Person数组生成的类型-> Person[]


interface Person {
   name: string
   age: number
}

type Workplace = Person[]

// nice ;)

祝你好运

你可以用一个索引器定义一个接口:

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

你不需要使用索引器(因为它的类型不太安全)。你有两个选择:

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(使用类而不是接口时更简单的迁移)。