0Pricing
Vue Academy · Lesson

Typing Composables and Refs

Ref , ComputedRef , typed return from composables, MaybeRef .

Typing the Composition API

Composables are just functions, and refs are just typed containers. Adding explicit types makes your composables safe to consume and gives callers full autocomplete.

Inferred ref Types

Most of the time TypeScript infers a ref type from its initial value. A ref(0) is a Ref<number> automatically.

import { ref } from 'vue'
const count = ref(0)        // Ref<number>
const name = ref('Coddy')  // Ref<string>

All lessons in this course

  1. TypeScript Setup in Vue Projects
  2. Typed Props and Emits with defineProps/defineEmits
  3. Typing Composables and Refs
  4. Generic Vue Components
← Back to Vue Academy