You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报错:Property 'find' does not exist on type 'object' 求助

解决TypeScript错误:Property 'find' does not exist on type 'object'.ts(2339)

错误根源

你遇到的问题和tsconfig.json的lib或target配置完全无关,核心问题是接口中favoriteReviews的类型定义错误:你把它标注为object类型,但find是数组(Array<T>)的专属方法,普通object并没有这个方法。

解决方案

修改你的接口定义,把favoriteReviews的类型从object改为包含_id属性的对象数组:

user?: {
    _id: string
    username: string
    favoriteReviews: { _id: string }[] // 改为数组类型,明确元素结构
}

如果favoriteReviews的元素有更多属性,可以单独定义一个接口来复用:

interface Review {
  _id: string
  // 可添加其他属性比如 content、rating 等
}

user?: {
    _id: string
    username: string
    favoriteReviews: Review[]
}

调整后,调用代码可以移除any类型,让TypeScript完成完整的类型检查:

const favoritedReview = user?.favoriteReviews?.find(
  (reviewItem) => reviewItem._id === review._id
)

为什么修改tsconfig无效?

你之前尝试调整lib或target配置,这些配置是用来指定TypeScript编译时支持的JavaScript语法和标准库,而你的问题是自定义类型的错误标注,和语言版本支持无关——哪怕把target设为esnext,object类型本身也不会拥有数组的find方法。

内容的提问来源于stack exchange,提问作者SavedCity

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 20:09:58