在使用 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