我有以下基本功能
custom.d.ts
:
import * as _vueIndex from "vue";
import * as _vue from "vue/types/vue";
declare global {
const Vue: _vueIndex.VueConstructor; // related to vue.d.ts "export const Vue: VueConstructor;"
namespace Vue {
//type Vue = typeof _vueIndex.default; // For some reason this becomes VueConstructor interface instead of Vue interface
type Vue = _vue.Vue;
type CreateElement = _vueIndex.CreateElement;
type CombinedVueInstance<Instance extends Vue, Data, Methods, Computed, Props> = _vue.CombinedVueInstance<Instance, Data, Methods, Computed, Props>;
/*Other interfaces*/
}
}
现在可以执行以下操作:
var app: Vue.CombinedVueInstance<Vue.Vue, any, VmMethods, any, any> = new Vue({
el: '#app',
methods: new VmMethods()
});
console.log(app.toggleSidebar()); // toggleSidebar is a public function in VmMethods
console.log(app.nonExisting()); // ReSharper gives correct cannot resolve error but TypeScript still transpiles for some reason, which gives a run time error
ReSharper仍然提供
Symbol Vue cannot be properly resolved, probably it is located in inaccessible module
但TypeScript可以传输并运行良好。