📖 Scenario: You are building a simple Vue 3 app that shows a user's full name by combining their first and last names.
🎯 Goal: Create a Vue 3 component using the Composition API where you define firstName and lastName as reactive variables, then create a typed computed property fullName that combines them. Display the full name in the template.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create reactive variables firstName and lastName with initial values
Create a typed computed property fullName that returns a string combining first and last names
Display fullName in the template inside a <p> tag
💡 Why This Matters
🌍 Real World
Typing computed properties helps you build reactive user interfaces that update automatically and safely with type checking.
💼 Career
Understanding typed computed properties is essential for Vue developers to write maintainable and bug-free code in professional projects.
Progress0 / 4 steps
1
Setup reactive variables
Inside the <script setup> block, import ref from 'vue' and create two reactive variables called firstName and lastName with initial values 'John' and 'Doe' respectively.
Vue
Hint
Use import { ref } from 'vue' and then const firstName = ref('John') and const lastName = ref('Doe').
2
Add typed computed property
Import computed from 'vue' and create a computed property called fullName that returns a string combining firstName.value and lastName.value separated by a space. Add a TypeScript type annotation to fullName to indicate it is a computed ref of type string.
Vue
Hint
Use computed and add the type ComputedRef<string> to fullName.
3
Display computed property in template
In the <template> section, add a <p> tag that displays the value of fullName using Vue's mustache syntax.
Vue
Hint
Use <p>{{ fullName }}</p> inside the template.
4
Add accessibility and responsiveness
Add an aria-label attribute to the <p> tag with the value 'User full name'. Also, add a class attribute with the value 'full-name' for styling. Then add a <style scoped> block that sets the font size of .full-name to 1.5rem and color to #333. This ensures the full name is accessible and looks good on all screen sizes.
Vue
Hint
Add aria-label="User full name" and class="full-name" to the <p> tag. Then add scoped CSS for .full-name.
Practice
(1/5)
1. What is the main benefit of typing computed properties in Vue?
easy
A. It disables reactivity for the computed property.
B. It helps catch errors early and improves code clarity.
C. It automatically updates the DOM without reactivity.
D. It makes the computed property run faster.
Solution
Step 1: Understand typing purpose
Typing in computed properties helps the developer know what type of value to expect and prevents mistakes.
Step 2: Recognize benefits
Typing improves code clarity and helps catch errors before running the code.
Final Answer:
It helps catch errors early and improves code clarity. -> Option B
A. The computed function should not use an arrow function.
B. The ref should be typed as string, not number.
C. The computed type should be number, not string.
D. The computed property cannot depend on a ref.
Solution
Step 1: Check the computed return value type
The expression count.value * 2 results in a number.
Step 2: Match computed generic type
The computed is typed as string, which mismatches the number result.
Final Answer:
The computed type should be number, not string. -> Option C
Quick Check:
Computed type must match return value type [OK]
Hint: Match computed generic type to returned value type [OK]
Common Mistakes:
Typing computed as string when returning number
Thinking arrow functions are invalid in computed
Believing computed can't depend on refs
5. You want to create a typed computed property that returns the count of items in a reactive array items. Which is the correct way to type it in Vue 3 with TypeScript?