在运行使用Nuxt.js
开发的Nuxt.js
时,我遇到了一个非常奇怪的错误,它有Vue.js
组件。也就是说,在运行应用程序时,我看到了与TypeScript
相关的错误,比如TS2749: 'About' refers to a value, but is being used as a type here. Did you mean 'typeof About'?
,尽管npm run test
没有显示任何内容。
我的带有抱怨行的spec.ts文件
import { shallowMount, Wrapper } from "@vue/test-utils";
import About from "@/pages/about.vue";
describe("About", () => {
const wrapper: Wrapper<About> = shallowMount(About); // <-- Complaining line
...
}
在设置输入之前,在突出显示类型时,类型应该很好,它将显示下面的类型。
建议的const wrapper: Wrapper<typeof About> = shallowMount(About);
解决方案会产生另一个TypeScript
错误,导致测试不编译。即TS2344: Type 'ExtendedVue<Vue, unknown, unknown, { setLocation: any; }, unknown>' does not satisfy the constraint 'Vue'. Type 'VueConstructor<{ setLocation: any; } & Vue>' is missing the following properties from type 'Vue': $el, $options, $parent, $root, and 32 more.
我不知道为什么test
会保持沉默,而TypeScript
在本地运行应用程序时就开始抱怨测试本身。他们都通过了顺便说一下,应用程序会编译。它只是与TypeScript
在@vue/test-utils
中的某种类型有关。
发布于 2021-04-12 10:10:14
Wrapper<About>
确实是一个问题--这是TS类型定义,而About
不是TS,type...it实际上是一个值(Vue组件定义对象)。
试试Wrapper<InstanceType<typeof About>>
显式声明来自外部库的类型(带有类型),并且实际上忽略TS类型推断,这感觉是很多不必要的工作。要做到这一点,你应该学习和理解排版
...which,坦白地说,我并没有完全理解...so,上面的代码可能是错误的:)
https://stackoverflow.com/questions/67055165
复制相似问题