Search code examples
objecttypescriptdestructuring

Why is my object desctructoring throwing a no string index signature error in typescript?


Given an object

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

I try to destructure it in typescript via:

const {brand} =  appConfig.brand;

Which fails via:

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

Solution

  • It's a faulty object desctructor syntax. These will work as expected:

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

    As they are a shortcut for:

    const brand =  appConfig.brand;
    const market =  appConfig.market;