Typing composables helps you catch mistakes early and understand your code better by telling Vue what kind of data your composable works with.
Typing composables in Vue
import { ref, Ref } from 'vue' function useCounter(): { count: Ref<number>, increment: () => void } { const count = ref(0) function increment() { count.value++ } return { count, increment } }
Use TypeScript to add types to your composable function's inputs and outputs.
Use Vue's Ref type to type reactive references.
import { ref, Ref } from 'vue' function useMessage(): { message: Ref<string>, setMessage: (newMsg: string) => void } { const message = ref('Hello') function setMessage(newMsg: string) { message.value = newMsg } return { message, setMessage } }
import { ref, Ref } from 'vue' function useToggle(initial: boolean = false): { state: Ref<boolean>, toggle: () => void } { const state = ref(initial) function toggle() { state.value = !state.value } return { state, toggle } }
This example shows a typed composable useCounter that returns a number ref and an increment function. We use it and print the count before and after incrementing.
import { ref, Ref } from 'vue' // Composable to manage a number counter function useCounter(): { count: Ref<number>, increment: () => void } { const count = ref(0) function increment() { count.value++ } return { count, increment } } // Using the composable const { count, increment } = useCounter() console.log(count.value) // 0 increment() console.log(count.value) // 1
Always import Vue types like Ref when typing reactive variables.
Typing composables improves code safety and editor support.
You can also type parameters your composable accepts for more flexibility.
Typing composables means adding TypeScript types to your Vue composable functions.
This helps catch errors and makes your code easier to understand.
Use Vue's Ref type for reactive data inside composables.
Practice
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 AQuick Check:
Typing composables = catch errors early [OK]
- Thinking typing improves app speed
- Confusing typing with bundle size reduction
- Believing typing removes reactive data
Solution
Step 1: Identify correct typing for reactive refs
Vue usesRef<Type>to type reactive refs, soRef<number>is correct for a number.Step 2: Check each option
const count: Ref<number> = ref(0); correctly typescountasRef<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 AQuick Check:
Use Ref<Type> for reactive refs [OK]
- Using reactive instead of Ref for refs
- Assigning ref() to plain types
- Mismatching value type and Ref generic
message inside the component?import { ref } from 'vue';
export function useMessage() {
const message = ref('Hello');
return { message };
}Solution
Step 1: Understand ref return type
Thereffunction returns aRefobject wrapping the value type, here a string.Step 2: Determine message type
Sincemessageis created byref('Hello'), its type isRef<string>.Final Answer:
Ref<string> -> Option BQuick Check:
ref('text') returns Ref<string> [OK]
- Assuming message is plain string
- Using Ref<any> without specifying type
- Confusing Ref<string> with string
import { ref, Ref } from 'vue';
export function useCounter(): { count: Ref<number> } {
const count = ref('0');
return { count };
}Solution
Step 1: Check ref argument type
The function expectscountto beRef<number>, butrefis called with a string '0'.Step 2: Identify mismatch error
This causes a type mismatch error because the initial value type does not match the declared Ref generic.Final Answer:
ref is called with a string instead of a number -> Option DQuick Check:
ref argument type must match Ref generic [OK]
- Passing wrong type to ref()
- Ignoring return type mismatch
- Confusing reactive and ref usage
title and a number count using Vue's Ref type?import { ref, Ref } from 'vue';
export function useData() {
const title = ref('My Title');
const count = ref(10);
return { title, count };
}Solution
Step 1: Identify types of returned refs
Bothtitleandcountare refs holding string and number respectively, so their types areRef<string>andRef<number>.Step 2: Match return type signature
The function return type should reflect these as an object with keys typed asRef<string>andRef<number>.Final Answer:
export function useData(): { title: Ref<string>; count: Ref<number> } -> Option CQuick Check:
Return type matches Ref types of reactive data [OK]
- Typing return values as plain string or number
- Using Ref<any> instead of specific types
- Using reactive instead of Ref in type
Ready to prove it?
Pass the test and this pattern moves unknown
