如何为对象定义枚举属性?

问题描述 投票:0回答:1
export enum JobType {
    'A' = 'A',
    'B' = 'B',
    'C' = 'C',
}

type RecipeObj = {
    job: JobType,
    input: {[key: string]: number},
    output: {[key: string]: number}
}


let recipe: RecipeObj = {
    job: JobType.A,
    input: {},
    output: {},
};

错误:在TS2322: Type 'string' is not assignable to type enum中将JobType.A分配给job时发生recipe。提供了一个提示:The expected type comes from property 'job' which is declared here on type 'RecipeObj',但我不明白在这种情况下它的用处。

编辑:在JobType.A

后固定逗号。
typescript enums typescript-typings
1个回答
0
投票

我没有收到与您相同的错误,但是您在JobType.A之后缺少逗号。

© www.soinside.com 2019 - 2024. All rights reserved.