0
0
Vueframework~10 mins

Typing composables in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Typing composables
Define composable function
Add TypeScript types to inputs/outputs
Use ref/reactive with typed values
Return typed state and methods
Consume composable with type safety
This flow shows how to create a Vue composable with TypeScript types to ensure inputs and outputs are correctly typed and safe to use.
Execution Sample
Vue
import { ref } from 'vue'

function useCounter(initial: number) {
  const count = ref<number>(initial)
  function increment() { count.value++ }
  return { count, increment }
}
A simple typed composable that manages a counter with a typed initial value and typed state.
Execution Table
StepActionInput/StateOutput/State Change
1Call useCounter with initial=5initial=5count = ref(5) with type number
2Define increment functioncount=ref(5)increment() ready to increase count.value
3Return { count, increment }count=ref(5)Composable returns typed count and increment
4Call increment()count.value=5count.value becomes 6
5Access count.valuecount.value=6Output is 6
💡 Composable returns typed state and methods; usage updates count.value correctly.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
count.valueundefined55566
Key Moments - 3 Insights
Why do we write ref<number>(initial) instead of just ref(initial)?
Typing ref<number> explicitly tells TypeScript the value inside is a number, ensuring type safety as shown in execution_table step 1.
What happens if increment() is called before count is initialized?
In this composable, count is initialized immediately, so increment() safely updates count.value as seen in step 4.
How does TypeScript help when using the returned composable?
TypeScript knows count is a Ref<number> and increment is a function, preventing mistakes when accessing or modifying them, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count.value after step 4?
Aundefined
B6
C5
D7
💡 Hint
Check the 'Output/State Change' column at step 4 in execution_table.
At which step is the increment function defined and ready to use?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing function definition in execution_table.
If we change initial to a string, what will happen to the typing?
Aincrement function will convert string to number automatically
Bcount.value will accept string without error
CTypeScript will show an error because count expects a number
DNo change, code runs fine
💡 Hint
Refer to the typing in the composable definition and variable_tracker for count.value type.
Concept Snapshot
Typing composables in Vue:
- Define composable function with typed parameters
- Use ref<T> or reactive with explicit types
- Return typed state and methods
- TypeScript ensures safe usage and catches errors
- Helps maintain clear contracts for composables
Full Transcript
Typing composables in Vue means adding TypeScript types to the inputs and outputs of your composable functions. You start by defining a composable function that takes typed parameters. Inside, you use Vue's ref or reactive with explicit type annotations to hold state. You then return the typed state and any methods. This approach helps TypeScript understand what types your composable expects and returns, so it can catch mistakes early. For example, a useCounter composable takes a number initial value, creates a ref<number> count, and returns count and an increment function. When increment is called, count.value increases safely. If you try to pass a wrong type, TypeScript will warn you. This makes your composables safer and easier to use.