按枚举值访问属性

时间:2019-04-24 13:29:56

标签: typescript angular5

我正在尝试通过TypeScript中的字符串变量访问属性。当我写d["account_type"]时一切正常,但是当我使用枚举Dictionary之一并写d[Dictionary.AccountType]时,我看到错误:

  

元素隐式地具有“ any”类型,因为类型“ Dictionaries”没有索引签名。

this.dictionaries.toPromise().then(d => { return d["account_type"] });

export enum Dictionary {
    AccountType = "account_type",
    AddressType = "address_type",
    CardBrand = "card_brand",
    ContactType = "contact_type",
    Country = "country",
    DevicePayDay = "device_pay_type",
    LogType = "log_type",
    PaymentProvider = "payment_provider",
    PaymentType = "payment_type",
    Permission = "permission",
    PointProperty = "point_property",
    PointService = "point_service",
    PromoSchemaOn = "promo_schema_on",
    PromoSchemaOff = "promo_schema_off",
    ReportType = "report_type",
}

是否可以通过枚举字符串值访问对象属性?

1 个答案:

答案 0 :(得分:0)

更改为

this.dictionaries.toPromise().then((d: any) => { return d[Dictionary.AccountType] });

演示:https://stackblitz.com/edit/angular-get-value-by-enum