正如您所知道的,从Vue 3开始,组件可以用TypeScript编写:
/// modal.vue
<template>
<div class="modal"></div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "Modal",
props: {
foo: String,
bar: String
},
mounted() {
this.$props.foo // how to type `this` out of this context?
}
});
</script>我的问题是,如何从defineComponent函数中键入vue实例?
/// another ts file.
let modal:???; // what the `???` should be?
modal.$props.foo // infer `$props.foo` correctly发布于 2020-09-21 12:09:13
我打算给出的“简单”答案是使用ReturnType<typeof defineComponent>,但是它不包含任何类型信息。当我开始研究如何在泛型方法中使用ReturnType时,我被stackoverflow rabbit hole where these seemed like something to explore迷住了
但是,在查看之后,vue有一个导出类型ComponentPublicInstance,可以相当容易地使用。ComponentPublicInstance也有一些不同的泛型参数。
import { ComponentPublicInstance } from 'vue';
let instance: ComponentPublicInstance<{ prop: string }, { value: string }>;https://stackoverflow.com/questions/63985658
复制相似问题