Vue3 Fundamentals ยท Vue 3 + TypeScript

Prop defaults

Prop defaults

A prop the parent may omit needs a value to fall back on, or the component renders nothing. In a <script setup lang="ts"> block you mark the prop optional with ? and give it a default by destructuring the props.

<script setup lang="ts">
const { label = 'Vue' } = defineProps<{ label?: string }>();
</script>

<template>
  <p>{{ label }}</p>
</template>

label?: string says the parent may leave it out. The = 'Vue' in the destructuring supplies the value when it does. Vue 3.5 keeps the destructured bindings reactive, so the default is not a one-time copy.

Before Vue 3.5 the same idea used withDefaults:

const props = withDefaults(defineProps<{ label?: string }>(), { label: 'Vue' });

Either way the default lives next to the type, so the optional prop and its fallback stay together.

In App.vue, give the optional label a default of Vue.

Challenge

The label disappears when the parent passes nothing. Export an App component from App.vue that declares an optional label prop and gives it a default of Vue, then renders it in a <p>.

The Test mounts your component without props and reads the paragraph. The starter declares the optional prop but no default, so the paragraph is empty and the Test fails.

App.vue
<script setup lang="ts">
const { label } = defineProps<{ label?: string }>();
</script>

<template>
  <p>{{ label }}</p>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.