Vue 3 App 的 Typescript 类型是什么?
What is the Typescript Type of the Vue 3 App?
因为 vue-test-utils 不为 Vue 3 提供“localvue”
我想到了使用 Vue 本身的 createApp。
现在我也在用Typescript
不知何故,我的 Linter 不关心这个声明:
const app = createApp(App);
但是当我想像这样为我的笑话测试拆分声明和实例化时:
let app;
describe("Test Vue", () => {
beforeEach(() => {
app = createApp(App);
});
});
它显然会抛出这个错误:
Variable 'app' implicitly has type 'any' in some locations where its type cannot be determined.ts(7034)
现在我觉得我很聪明,就拿 createApp 说的类型 returns:
export declare const createApp: CreateAppFunction<Element>;
像这样:
let app: CreateAppFunction<Element>;
describe("Test Vue", () => {
beforeEach(() => {
app = createApp(App); //***declared Module. See End for declaration
});
});
但这会 return:
Type 'App<Element>' is not assignable to type 'CreateAppFunction<Element>'.
Type 'App' 没有提供签名的匹配项 '(rootComponent: Component, rootProps?: Data | null | undefined): App'.ts(2322 )
所以我想知道:应用程序的类型是什么?
***应用声明
declare module "*.vue" {
import type { DefineComponent } from "vue";
const component: DefineComponent<{}, {}, any>;
export default component;
}
原来是这样的:
import { createApp, App } from "vue";
import Application from "@/App.vue";
let app: App;
describe("Test Vue", () => {
beforeEach(() => {
app = createApp(Application);
});
所以 createApp 创建的应用类型是 import { App } from "vue";
因为 vue-test-utils 不为 Vue 3 提供“localvue” 我想到了使用 Vue 本身的 createApp。
现在我也在用Typescript
不知何故,我的 Linter 不关心这个声明:
const app = createApp(App);
但是当我想像这样为我的笑话测试拆分声明和实例化时:
let app;
describe("Test Vue", () => {
beforeEach(() => {
app = createApp(App);
});
});
它显然会抛出这个错误:
Variable 'app' implicitly has type 'any' in some locations where its type cannot be determined.ts(7034)
现在我觉得我很聪明,就拿 createApp 说的类型 returns:
export declare const createApp: CreateAppFunction<Element>;
像这样:
let app: CreateAppFunction<Element>;
describe("Test Vue", () => {
beforeEach(() => {
app = createApp(App); //***declared Module. See End for declaration
});
});
但这会 return:
Type 'App<Element>' is not assignable to type 'CreateAppFunction<Element>'.
Type 'App' 没有提供签名的匹配项 '(rootComponent: Component
所以我想知道:应用程序的类型是什么?
***应用声明
declare module "*.vue" {
import type { DefineComponent } from "vue";
const component: DefineComponent<{}, {}, any>;
export default component;
}
原来是这样的:
import { createApp, App } from "vue";
import Application from "@/App.vue";
let app: App;
describe("Test Vue", () => {
beforeEach(() => {
app = createApp(Application);
});
所以 createApp 创建的应用类型是 import { App } from "vue";