为什么我的对象析构函数在打字稿中抛出无字符串索引签名错误?

时间:2016-12-05 17:02:47

标签: object typescript destructuring

给定一个对象

const appConfig: {
    brands: {...},
    market: {...},
}

我尝试通过以下方式在打字稿中对其进行解构:

const {brand} =  appConfig.brand;

通过以下方式失败:

src/partner/transform.ts(17,12): error TS2459: Type 'IBrandConfig' has no property 'brand' and no string index signature.

1 个答案:

答案 0 :(得分:3)

这是一个错误的对象析构函数语法。这些将按预期工作:

const {brand} =  appConfig;
const {brand, market} =  appConfig;

因为它们是:

的捷径
const brand =  appConfig.brand;
const market =  appConfig.market;
相关问题