📖 Scenario: You are building a small Vue 3 app that uses a composable function to manage a counter. You want to add TypeScript types to your composable to make it safer and easier to use.
🎯 Goal: Create a typed composable function called useCounter that returns a reactive count and an increment function. Use TypeScript types to define the return values.
📋 What You'll Learn
Create a reactive variable count with initial value 0
Create a composable function useCounter that returns count and an increment function
Add TypeScript types to the composable's return object
Use Vue 3 Composition API with ref and readonly
💡 Why This Matters
🌍 Real World
Typed composables help build reusable and safe logic blocks in Vue 3 apps, improving code quality and developer experience.
💼 Career
Understanding how to type composables is essential for Vue developers working with TypeScript in professional projects.
Progress0 / 4 steps
1
Setup a reactive count variable
Create a reactive variable called count using Vue's ref function and set its initial value to 0.
Vue
Hint
Use const count = ref(0) to create a reactive number starting at zero.
2
Create the useCounter composable function
Create a function called useCounter that returns an object with count and an increment function. The increment function should increase count.value by 1.
Vue
Hint
Define useCounter as a function that returns count and increment. The increment function adds 1 to count.value.
3
Add TypeScript types to the composable return
Add TypeScript types to the useCounter function so it returns an object with count typed as Ref<number> and increment typed as a function returning void. Import Ref from 'vue'.
Vue
Hint
Use Ref<number> to type count and () => void to type increment.
4
Make count readonly in the composable return
Import readonly from 'vue' and return count wrapped with readonly in the useCounter function to prevent external modification of count.
Vue
Hint
Use readonly(count) to protect count from outside changes.
Practice
(1/5)
1. What is the main purpose of typing composables in Vue with TypeScript?
easy
A. To catch errors early and improve code understanding
B. To make the app run faster
C. To reduce the size of the final bundle
D. To avoid using reactive data
Solution
Step 1: Understand typing benefits
Typing helps catch mistakes before running the code and clarifies what data types are used.
Step 2: Compare options
Only To catch errors early and improve code understanding correctly states the purpose of typing composables; others are unrelated benefits.
Final Answer:
To catch errors early and improve code understanding -> Option A
Quick Check:
Typing composables = catch errors early [OK]
Hint: Typing helps find mistakes before running code [OK]
Common Mistakes:
Thinking typing improves app speed
Confusing typing with bundle size reduction
Believing typing removes reactive data
2. Which of the following is the correct way to type a reactive number in a Vue composable using TypeScript?
easy
A. const count: Ref<number> = ref(0);
B. const count: reactive<number> = reactive(0);
C. const count: number = ref(0);
D. const count: Ref = ref('0');
Solution
Step 1: Identify correct typing for reactive refs
Vue uses Ref<Type> to type reactive refs, so Ref<number> is correct for a number.
Step 2: Check each option
const count: Ref<number> = ref(0); correctly types count as Ref<number>. const count: reactive<number> = reactive(0); uses wrong syntax, C assigns ref to number type, D uses wrong type and string value.
Final Answer:
const count: Ref<number> = ref(0); -> Option A
Quick Check:
Use Ref<Type> for reactive refs [OK]
Hint: Use Ref<Type> to type reactive refs in composables [OK]
Common Mistakes:
Using reactive instead of Ref for refs
Assigning ref() to plain types
Mismatching value type and Ref generic
3. Given this composable code, what is the type of message inside the component?
import { ref } from 'vue';
export function useMessage() {
const message = ref('Hello');
return { message };
}
medium
A. string
B. Ref<string>
C. Ref<any>
D. any
Solution
Step 1: Understand ref return type
The ref function returns a Ref object wrapping the value type, here a string.
Step 2: Determine message type
Since message is created by ref('Hello'), its type is Ref<string>.
Final Answer:
Ref<string> -> Option B
Quick Check:
ref('text') returns Ref<string> [OK]
Hint: ref('text') returns Ref<string> type [OK]
Common Mistakes:
Assuming message is plain string
Using Ref<any> without specifying type
Confusing Ref<string> with string
4. Identify the error in this typed composable code: