如何在 vuejs 3 中设置 createApp(App) 函数的类型?

How to set type for createApp(App) function in vuejs 3?

我在 typescript 上使用 vue3 和 firebase。

// main.ts
import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import { auth } from './firebase'

// eslint-disable-next-line
let app: any

auth.onAuthStateChanged(() => {
  if (!app) {
    app = createApp(App)
      .use(store)
      .use(router)
      .mount('#app')
  }
})

我应该为 'app' 变量设置什么类型?我尝试了 App,typeof App 但失败了。

createApp returns 一个 App instance. Since you already have an App imported, you need to rename the type while importing. But notice app.mount returns a different type,你不能在 createApp 之后链接它而不改变类型:

import { createApp, App as Application } from 'vue'
import App from './App.vue'
// other imports

let app: Application 

auth.onAuthStateChanged(() => {
  if (!app) {
    app = createApp(App)
    app.use(store)
    app.use(router)
    app.mount('#app')
  }
})