Composables — typed reusable logic.
1// composables/useCounter.ts2import { ref, computed } from "vue"34interface UseCounterOptions {5 initialValue?: number6 step?: number7}89interface UseCounterReturn {10 count: Ref<number>11 doubled: ComputedRef<number>12 increment: () => void13 decrement: () => void14}1516export function useCounter(17 options: UseCounterOptions = {}18): UseCounterReturn {19 const { initialValue = 0, step = 1 } = options20 const count = ref(initialValue)21 const doubled = computed(() => count.value * 2)2223 function increment() {24 count.value += step25 }2627 function decrement() {28 count.value -= step29 }3031 return { count, doubled, increment, decrement }32}