Subscribe on changes!

withDefaults should work with an object literal reference

avatar
Jun 8th 2023

What problem does this feature solve?

Currently this does not work in my code:

const defaultsObject = {  name: "Evan You" }
// also: var defaultsObject = {  name: "Evan You" }
// also: let defaultsObject = {  name: "Evan You" }

const props = withDefaults(defineProps<{name: string}>(), defaultsObject)

This gives the following error: The 2nd argument of withDefaults must be an object literal.

There is a good change I am just doing something wrong, if not:

Since we can now use interfaces for the props it would be really nice to be able to pass an object to the defaults, it was also a suggestion on the topic The added value is having the defaults also shared through composables if they are the same in different components.

What does the proposed API look like?

import { inputPropertiesInterface, inputDefaultsObject } from '../common/Input';

const props = withDefaults(defineProps<inputPropertiesInterface>(), inputDefaultsObject)
avatar
Jun 11th 2023

We have to hoist defaultsObject like this Vue Playground

avatar
Jun 11th 2023

Maybe we can hoist the variables if they are only used in macros.