-
I am currently facing an issue with defining default values for an optional prop. What kinda throws me of is that I am pretty sure this worked in the past. I already searched for 'LooseRequired' in issues and discussions and on Stackoverflow but could not find anything that looked relevant. Here is a minimal example: <script setup lang="ts">
type SomeOptionalProp = {
name: string;
};
type Props = {
someOptionalProp?: SomeOptionalProp;
};
withDefaults(defineProps<Props>(), {
someOptionalProp: {
name: 'Test',
},
});
</script> This throws an 'Object literal may only specify known properties' error. You can check out the example in Stackblitz At first I thought this might be because the default is an untyped object and if I would assert the type then the error would be resolved. When I assert the default like this withDefaults(defineProps<Props>(), {
someOptionalProp: {
name: 'Test',
} as SomeOptionalProp,
}); the error message changes to:
Why is this happening? To me everything looks perfectly fine. Do I need to wrap SomeOptionalProp in some kind of helper type? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I forgot to define the default as an arrow function: withDefaults(defineProps<Props>(), {
someOptionalProp: () => ({
name: 'Test',
}),
}); |
Beta Was this translation helpful? Give feedback.
I forgot to define the default as an arrow function: