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
相关产品推荐
相关产品推荐

