Vue: Provide a way to type an instance method when using typescript

Created on 10 May 2017  路  4Comments  路  Source: vuejs/vue

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'

Most helpful comment

@gnumarcelo This code worked for me only when I put it in the same plugin file..

Here is my axio-plugin.ts file:

import _Vue from 'vue';
import Axios from 'axios';

export function AxiosPlugin<AxiosPlugOptions>(Vue: typeof _Vue, options?: AxiosPluginOptions): void {
  // do stuff with options
  Vue.prototype.$http = Axios;
}

export class AxiosPluginOptions {
  // add stuff
}

import { AxiosStatic } from 'axios';

declare module 'vue/types/vue' {
  interface Vue {
    $http: AxiosStatic;
  }
}

All 4 comments

@gnumarcelo This code worked for me only when I put it in the same plugin file..

Here is my axio-plugin.ts file:

import _Vue from 'vue';
import Axios from 'axios';

export function AxiosPlugin<AxiosPlugOptions>(Vue: typeof _Vue, options?: AxiosPluginOptions): void {
  // do stuff with options
  Vue.prototype.$http = Axios;
}

export class AxiosPluginOptions {
  // add stuff
}

import { AxiosStatic } from 'axios';

declare module 'vue/types/vue' {
  interface Vue {
    $http: AxiosStatic;
  }
}

@gnumarcelo This code worked for me only when I put it in the same plugin file..

Here is my axio-plugin.ts file:

import _Vue from 'vue';
import Axios from 'axios';

export function AxiosPlugin<AxiosPlugOptions>(Vue: typeof _Vue, options?: AxiosPluginOptions): void {
  // do stuff with options
  Vue.prototype.$http = Axios;
}

export class AxiosPluginOptions {
  // add stuff
}

import { AxiosStatic } from 'axios';

declare module 'vue/types/vue' {
  interface Vue {
    $http: AxiosStatic;
  }
}

讗讞 砖诇讬 住讬讚专转 讗讜转讬 讘讜诪讘讛
(some thank you words in Hebrew)

Was this page helpful?
0 / 5 - 0 ratings