通过枚举值访问属性

问题描述 投票:0回答:1

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

元素隐式具有“任意”类型,因为类型“字典”没有索引签名。

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",
}

有没有办法通过枚举字符串值访问对象属性?

typescript angular5
1个回答
0
投票

改成

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

但是:ぁzxswい

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