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

Vue Test Utils+TypeScript中wrapper.vm属性类型报错解决方法

Vue Test Utils + TypeScript:解决wrapper.vm无法识别自定义ref属性的TS报错问题

在使用TypeScript结合Vue Test Utils进行组件测试时,若尝试通过以下代码修改组件内的ref值:

wrapper.vm.aCoolRefValueToManipulate = 'something much cooler'

功能可正常运行,但TypeScript会抛出类型错误,提示类似:

Property 'showTopDown' does not exist on type '{ $: ComponentInternalInstance; ... }'.ts(2339)

这是因为TypeScript无法识别wrapper.vm上的自定义属性。

解决方案

通过类型断言将wrapper.vm转为any类型,即可绕过TypeScript的类型校验,代码如下:

(wrapper.vm as any).aCoolRefValueToManipulate = 'something much cooler'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:05:24