【问题标题】:TypeScript: TS2339 error -- Property does not exist on type 'object'TypeScript:TS2339 错误——“对象”类型上不存在属性
【发布时间】:2020-08-07 03:12:58
【问题描述】:

我无法在我的应用上解决此错误。 本质上,我的界面设置如下:

interface skuInfo {
    href: string
}

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

让我感到困惑的是myObjectItem.images.sku

我有一个对象设置为:const exObj: myObjectItem

当我尝试访问它的属性时: exObj.images.sku[0].href,我收到以下错误:

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

在我的 Visual Code 编辑器中,我可以看到它在访问对象数组时预填充了 href 属性,但 TS 仍在抱怨。

我错过了什么吗?

【问题讨论】:

    标签: typescript typescript-typings typescript-types typescript-3.6


    【解决方案1】:

    根据你的界面,属性访问应该是

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

    所以,typescript 是对的,你的 images 对象没有 href 属性

    检查此playground,您的错误不会重现。

    【讨论】:

    • 对不起,我重新编辑了原来的 cmets。是的,我的意思是images.sku[0].href,但它仍然给我一个错误
    • 我添加了一个游乐场
    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 2018-01-29
    • 2020-07-07
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多