TypeScript: TS2339 error -- Property does not exist on type 'object'

1.7k Views Asked by At

I am having trouble resolving this error on my app. Essentially, I have an interface set as so:

interface skuInfo {
    href: string
}

interface myObjectItem {
    itemId: string
    isFound: boolean
    price: {
        selling: number 
    }
    images: {
        sku: skuInfo[]
    }
}

What trips me is the myObjectItem.images.sku.

I have an object set as so: const exObj: myObjectItem.

When I try to access its property: exObj.images.sku[0].href, I receive the following error:

Property 'href' does not exist on type 'object'

In my Visual Code editor, I can see that it prefills the href attribute when accessing an array of the object, but TS is still complaining.

Am I missing something?

1

There are 1 best solutions below

2
felixmosh On

Based on your interface, the property access should be

exObj.images.sku[0].href
// -----------^

So, typescript is right, your images object doesn't have href property

Check this playground, your error won't reproduces.