-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Closed
Description
Version
2.3.3
Reproduction link
https://github.com/lucastheisen/vue-instance-method-fail
Steps to reproduce
- Use typescript
- Create a plugin that adds an instance method (https://vuejs.org/v2/guide/plugins.html, example 4):
export class AppVuePlugin {
public static install(vue: typeof Vue, options: any) {
Object.defineProperties(vue.prototype, {
$api: {
get() { return new AppApi() },
},
})
}
}
- Install the plugin:
Vue.use(AppVuePlugin);
- Try to use it:
@Component({})
export default class AppComponent extends Vue {
public data: AppData
public created() {
this.data = this.$api.getAppData(); // <- Property '$api' does not exist on type 'AppData'
}
What is expected?
I expect the error but would like to see an official way to allow for this...
What is actually happening?
The ts build error:
ERROR in C:\Users\ltheisen\git\pastdev-vue-instance-method-fail\src\app.vue.ts
(33,22): error TS2339: Property '$api' does not exist on type 'App'.
Not sure the best approach for this, perhaps allowing some sort of generic on the Vue class? Or an official documented suggestion for working around this... Right now i create a subclass, but it feels wrong:
import Vue from 'vue'
export default class AppVue extends Vue {
public $api: AppApi
}
And then use this version of Vue
everywhere else:
import Vue from './app-vue'
Metadata
Metadata
Assignees
Labels
No labels