A composable is a reusable function that uses Vue's Composition API to share reactive state and logic across components.
Click to reveal answer
beginner
Why do we type composables in Vue with TypeScript?
Typing composables helps catch errors early, improves code completion, and makes the code easier to understand and maintain.
Click to reveal answer
intermediate
How do you type the return value of a composable that returns a ref in Vue?
You specify the type inside the ref, for example: const count = ref<number>(0); and the composable returns { count } typed accordingly.
Click to reveal answer
intermediate
What TypeScript utility type can help infer the return type of a composable function?
The ReturnType<T> utility type extracts the return type of a function type T, useful for typing composables.
Click to reveal answer
intermediate
How can you type a composable that accepts parameters?
You define the parameter types explicitly in the function signature, for example: function useCounter(initial: number): { count: Ref<number> }.
Click to reveal answer
What does a Vue composable typically return?
ACSS styles
BReactive state and functions
CHTML templates
DVue directives
✗ Incorrect
Composables return reactive state and functions to share logic across components.
How do you type a ref holding a string in a Vue composable?
Aref<boolean>('')
Bref<number>('')
Cref<string>('')
Dref<any>('')
✗ Incorrect
You specify the type inside ref, so ref('') creates a reactive string.
Which TypeScript feature helps infer the return type of a composable?
AReturnType
BPartial
CPick
DReadonly
✗ Incorrect
ReturnType extracts the return type of a function, useful for composables.
Why is typing parameters in composables important?
ATo generate HTML automatically
BTo make the code run faster
CTo add CSS styles
DTo catch errors and improve code clarity
✗ Incorrect
Typing parameters helps catch mistakes early and makes code easier to understand.
What is the main benefit of using composables in Vue?
AReusing logic and state across components
BWriting inline CSS
CCreating new HTML tags
DReplacing Vue directives
✗ Incorrect
Composables let you reuse logic and reactive state easily in multiple components.
Explain how to type a Vue composable that returns a reactive count and a function to increment it.
Think about typing the ref and the function inside the composable.
You got /3 concepts.
Describe why typing parameters and return values in composables improves Vue app development.
Consider benefits of TypeScript in general applied to composables.
You got /4 concepts.
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: