Vue3 Fundamentals · Vue 3 + TypeScript

Typing props

Typed props

Typing props

A child component receives data through props, and in a <script setup lang="ts"> block you declare them with the type-only defineProps<…>() form. The type is the source of truth: Vue reads it and builds the runtime prop declaration for you, so there is no second list to keep in sync.

<script setup lang="ts">
const props = defineProps<{ user: { name: string } }>();
</script>

<template>
  <p>{{ props.user.name }}</p>
</template>

The type describes the shape the parent must pass. A missing name, or a user that is not an object, is a compile error in the parent, caught before the component renders.

Because the type is the only declaration, the object you receive is typed all the way down. props.user is { name: string }, so props.user.name is a string and the editor completes it for you. Rendering props.user directly would show [object Object] — the type is what tells you the field is there.

In App.vue, type the user prop and render its name.

Challenge

The card prints the whole user object instead of the name. Export an App component from App.vue that declares a user prop with the type-only defineProps<{ user: { name: string } }>() form and renders props.user.name inside a <p>.

The Test mounts your component with { user: { name: 'Ada' } } and reads the paragraph. The starter renders the object itself, so the paragraph reads [object Object] and the Test fails.

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

<template>
  <p>{{ props.user }}</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.