📖 Scenario: You are building a simple Vue 3 component that shows a user's profile card. You want to make sure the component receives the correct types of data as props.
🎯 Goal: Create a Vue 3 component using the Composition API with <script setup> that accepts typed props for name (string), age (number), and isMember (boolean). Display these props in the template.
📋 What You'll Learn
Create a Vue 3 component with <script setup> syntax
Define props with explicit types: name as string, age as number, isMember as boolean
Use the props in the template to show the user's name, age, and membership status
Use the Composition API style for props typing
💡 Why This Matters
🌍 Real World
Typing props helps catch errors early and makes components easier to use and maintain in real projects.
💼 Career
Understanding how to type props is essential for Vue developers to build reliable and scalable applications.
Progress0 / 4 steps
1
Setup the component and define props object
Create a Vue 3 component with <script setup> and define a props object with keys name, age, and isMember without types yet.
Vue
Hint
Start by creating a props object with keys name, age, and isMember. You don't need to add types yet.
2
Add types to the props using defineProps
Replace the props object with defineProps and specify types: name as String, age as Number, and isMember as Boolean.
Vue
Hint
Use defineProps to declare the props with their types.
3
Display the props in the template
In the <template>, display the user's name, age, and isMember status inside paragraphs.
Vue
Hint
Use mustache syntax {{ }} to show each prop inside a paragraph.
4
Add default values for props
Modify the defineProps call to add default values: name default to "Guest", age default to 0, and isMember default to false.
Vue
Hint
Use the object form for each prop to add a default value.
Practice
(1/5)
1. What is the main benefit of typing component props in Vue with TypeScript?
easy
A. It disables prop validation.
B. It automatically styles the component.
C. It makes the component load faster.
D. It helps catch errors early and improves code clarity.
Solution
Step 1: Understand the purpose of typing props
Typing props allows the developer to specify what kind of data the component expects, which helps catch mistakes before running the code.
Step 2: Recognize the benefits of typing
Typed props improve code clarity and reduce bugs by making expectations explicit.
Final Answer:
It helps catch errors early and improves code clarity. -> Option D
Quick Check:
Typing props = catch errors early [OK]
Hint: Typing props means fewer bugs and clearer code [OK]
Common Mistakes:
Thinking typing changes component speed
Confusing typing with styling
Believing typing disables validation
2. Which is the correct syntax to type props in a Vue 3 <script setup lang="ts"> block?
easy
A. const props = defineProps('title: string')
B. const props = defineProps(["title: string"])
C. const props = defineProps<{ title: string }>()
D. const props = defineProps(title: string)
Solution
Step 1: Recall the correct typing syntax
In Vue 3 with TypeScript, props are typed using defineProps<Type>() where Type is an object type.
Step 2: Identify the correct syntax
const props = defineProps<{ title: string }>() uses the correct generic syntax with an object type describing the prop.
Final Answer:
const props = defineProps<{ title: string }>() -> Option C
A. Props should not use union with undefined; use optional instead.
B. Missing default value for name prop.
C. defineProps must be called without type arguments.
D. The prop name must be a number, not string.
Solution
Step 1: Understand how to mark optional props
In Vue with TypeScript, optional props should be marked with ? instead of union with undefined.
Step 2: Identify the incorrect typing
Using string | undefined is allowed but not recommended; better to write name?: string for optional props.
Final Answer:
Props should not use union with undefined; use optional instead. -> Option A
Quick Check:
Use ? for optional props, not union with undefined [OK]
Hint: Use ? to mark optional props, not union with undefined [OK]
Common Mistakes:
Using union with undefined instead of ?
Forgetting to mark optional props
Confusing default values with typing
5. You want to type a Vue component prop named items that accepts an array of objects with a required id (number) and optional label (string). Which is the correct TypeScript type for defineProps?
hard
A. defineProps<{ items?: Array<{ id: number; label: string }> }>()
B. defineProps<{ items: { id: number; label?: string }[] }>()
D. defineProps<{ items: Array<{ id: number; label: string }> }>()
Solution
Step 1: Understand the prop structure
The prop items is an array of objects. Each object must have a required id (number) and an optional label (string).
Step 2: Choose the correct typing
defineProps<{ items: { id: number; label?: string }[] }>() correctly types items as an array of objects with id: number and optional label?: string. The prop itself is required.