0
0
Vueframework~20 mins

Typing component props in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Props Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component with typed props?

Consider this Vue 3 component using <script setup> and typed props. What will it render when used as <Greeting name="Alice" age=30 />?

Vue
<script setup lang="ts">
import { defineProps } from 'vue'

const props = defineProps<{ name: string; age: number }>()
</script>
<template>
  <p>Hello, {{ props.name }}! You are {{ props.age }} years old.</p>
</template>
A<p>Hello, Alice! You are 30 years old.</p>
B<p>Hello, {{ name }}! You are {{ age }} years old.</p>
C<p>Hello, Alice! You are {{ age }} years old.</p>
D<p>Hello, ! You are 30 years old.</p>
Attempts:
2 left
💡 Hint

Remember that defineProps creates a typed props object you can use directly in the template.

📝 Syntax
intermediate
2:00remaining
Which option correctly types a prop with a default value in Vue 3?

Given a prop count that should be a number with a default of 10, which option correctly types and sets the default in a Vue 3 <script setup lang="ts"> component?

A
const props = defineProps&lt;{ count: number }&gt;()
const count = props.count ?? 10
B
const props = defineProps&lt;{ count?: number }&gt;()
const count = props.count ?? 10
Cconst props = defineProps({ count: { type: Number, default: 10 } })
D
const props = defineProps&lt;{ count: number }&gt;()
const count = props.count || 10
Attempts:
2 left
💡 Hint

Optional props need ? in the type and a fallback value in code.

🔧 Debug
advanced
2:00remaining
Why does this Vue component cause a TypeScript error?

Examine this Vue 3 component snippet. Why does TypeScript report an error on props.title.toUpperCase()?

Vue
<script setup lang="ts">
const props = defineProps<{ title?: string }>()

const upperTitle = props.title.toUpperCase()
</script>
ABecause <code>props</code> is not defined in <code>&lt;script setup&gt;</code>.
BBecause <code>defineProps</code> does not support optional props.
CBecause <code>title</code> is optional and might be undefined, so calling <code>toUpperCase()</code> is unsafe.
DBecause <code>toUpperCase()</code> is not a valid string method in TypeScript.
Attempts:
2 left
💡 Hint

Think about what happens if title is missing.

state_output
advanced
2:00remaining
What is the value of count after this Vue component runs?

Given this Vue 3 component using typed props and a reactive state, what is the final value of count after mounting?

Vue
<script setup lang="ts">
import { ref, watch } from 'vue'

const props = defineProps<{ initialCount: number }>()
const count = ref(props.initialCount)

watch(() => props.initialCount, (newVal) => {
  count.value = newVal * 2
}, { immediate: true })
</script>

<!-- Used as: <Counter initialCount=5 /> -->
A5
Bundefined
C0
D10
Attempts:
2 left
💡 Hint

Consider how the watcher updates count when initialCount changes.

🧠 Conceptual
expert
2:00remaining
Which statement about typing Vue component props with TypeScript is TRUE?

Choose the correct statement about typing props in Vue 3 with TypeScript and <script setup>.

AProps typed with <code>defineProps&lt;T&gt;()</code> are only checked at compile time; runtime validation requires additional options.
BUsing <code>defineProps&lt;T&gt;()</code> enforces prop types at runtime automatically without extra code.
CYou cannot use optional props with <code>defineProps&lt;T&gt;()</code> in Vue 3.
DVue 3 automatically converts all prop types to strings when using TypeScript.
Attempts:
2 left
💡 Hint

Think about the difference between TypeScript checks and Vue runtime behavior.