Skip to content

Provide a way to type an instance method when using typescript #5641

@lucastheisen

Description

@lucastheisen

Version

2.3.3

Reproduction link

https://github.com/lucastheisen/vue-instance-method-fail

Steps to reproduce

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions