Property ‘$test‘ does not exist on type ‘ComponentPublicInstance
eviroment
vue3 + ts
problem
期望使用全局变量
// main.ts
app.config.globalProperties.$test = 123
// test.vue
console.log(`this.$test`, this.$test)
error
Property ‘$test‘ does not exist on type ‘ComponentPublicInstance
solution
定义$test的类型
创建文件 src/shims-core.d.ts
import { ComponentCustomProperties } from "vue";
declare module "@vue/runtime-core" {
interface ComponentCustomProperties {
$test: number,
}
}