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

Vue & TypeScript响应式对象加嵌套函数时嵌套Ref参数报错问题

Vue响应式对象中嵌套Ref自动解包的TypeScript类型问题

在Vue 2.7及3版本中,根据官方文档说明,嵌套在ref或reactive对象内的ref会被自动解包。以下示例代码运行正常,无TypeScript类型错误:

interface IName {
  first: string;
  last: string;
  sayHi?: () => void;
}

interface IPerson {
 name: IName;
 isAdult: boolean;
}

const me: IPerson = reactive({
  isAdult: ref(false),
  name: {
    first: ref("first"),
    last: "last"
  },
});

但当在响应式对象me的任意层级添加任意函数(如下示例中添加sayHi函数)时,会触发TypeScript类型错误:

const me: IPerson = reactive({
  isAdult: ref(false), // Type 'Ref<boolean>' is not assignable to type 'boolean'.ts(2322)
  name: {
    first: ref("first"), // Type 'Ref<string>' is not assignable to type 'string'.ts(2322)
    last: "last"
  },
  sayHi: function () {
    console.log("Hi !")
  }
});

奇怪的是,项目实际构建运行时并无任何错误。请问这是TypeScript的Bug、限制,还是其他原因导致的?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 13:22:07