[Vue3] 关于vue3+ts中使用props进行 interface 类型限制设置默认值报错问题(props: Readonly<Props>))
Type '{}' is not assignable to type '(props: Readonly) => object'.Type '{}' provides no match for the signature '(props: Readonly): object'
·
在使用 withDefaults
时,为自定义类声明默认值时报错。
报错信息:
Type '{}' is not assignable to type '(props: Readonly<Props>) => object'. Type '{}' provides no match for the signature '(props: Readonly<Props>): object'.
报错时的代码:
//props
interface Props {
mydata:object,
}
const props = withDefaults(defineProps<Props>(), {
mydata:{}
});
解决后的代码:
//props
interface Props {
mydata: object;
}
const props = withDefaults(defineProps<Props>(), {
mydata: () => {
return {};
},
});
我的示例:
vue官方代码地址:https://cn.vuejs.org/guide/typescript/composition-api.html#typing-component-props
更多推荐
所有评论(0)