首页 > 解决方案 > 如何在模块扩充中引用 ComponentOptions 的其他泛型参数?

问题描述

这是我想在我的.d.ts文件中执行的操作:

declare module 'vue/types/options' {
  interface ComponentOptions<V extends Vue> {
    beforeInstance?: (options: ComponentOptions<V>) => void | Promise<(vm: CombinedVueInstance<V, Data, Methods, Computed, Props>) => any | undefined>;
  }
}

它没有工作,因为它不知道Data,Methods和。我已经尝试将这些变量放入如下:ComputedPropsComponentOptions

declare module 'vue/types/options' {
  interface ComponentOptions<V extends Vue, Data=DefaultData<V>, Methods=DefaultMethods<V>, Computed=DefaultComputed, PropsDef=PropsDefinition<DefaultProps>, Props=DefaultProps> {
    beforeInstance?: (options: ComponentOptions<V>) => void | Promise<(vm: CombinedVueInstance<V, Data, Methods, Computed, Props>) => any | undefined>;
  }
}

它会破坏我的组件定义中的类型推断:

export default Vue.extend({
  data: function () {
    return {
      value: 'abc'
    };
  },
  mounted: function () {
    console.log(this.value); // This does not work.
  }
});

标签: typescriptvue.js

解决方案


推荐阅读