TypeScript Support
🎯 Introduction
By the end of this article, you will be comfortable using TypeScript in Vue 3 to improve type safety and productivity. You will work with prop and event type inference, add types to reusable composables, and make use of type hints while debugging to write more robust, maintainable frontend code.
⚙️ Improved TypeScript Support in Vue 3
Vue 3 was designed with TypeScript support in mind. Its core is written in TypeScript, providing comprehensive official type definitions. This brings several benefits:
- Accurate inference for component props and emitted events reduces repetitive manual type declarations.
- The Composition API is designed to work well with TypeScript, making it straightforward to use in setup functions.
- Both Vue CLI and Vite templates support TypeScript, making typed projects quick to set up.
- Richer type inference and editor hints improve the development experience.
🧩 TypeScript in Components (Prop and Event Type Inference)
Typing props and emitted events is central to using TypeScript in Vue 3 components. The defineProps and defineEmits compiler macros provide concise, safe declarations from which types can be inferred.
<template>
<button @click="handleClick">{{ label }}</button>
</template>
<script setup lang="ts">
const props = defineProps<{
label: string;
count?: number;
}>();
const emit = defineEmits<{
(e: 'click', id: number): void;
}>();
function handleClick() {
emit('click', 42);
}
</script>
In this code:
definePropsaccepts a type declaration for the props:labelis a required string, andcountis an optional number.defineEmitsdeclares aclickevent with a numeric argument.- Full type hints are available in both the template and the component logic.
Compared with Vue 2 patterns using PropType and $emit, this is more concise and provides more precise types.
lang="ts" with these typed defineProps and defineEmits declarations so that TypeScript syntax and inference work correctly.🧑🔧 TypeScript in Composables
Composables are central to Vue 3's Composition API, encapsulating reusable application logic. TypeScript constrains their inputs and return values, making them safer to call.
Example: a counter composable
import { ref } from 'vue';
export function useCounter(initialValue = 0) {
const count = ref<number>(initialValue);
function increment(amount: number = 1) {
count.value += amount;
}
return { count, increment };
}
Usage:
import { useCounter } from './useCounter';
const { count, increment } = useCounter(10);
increment(5);
// count.value has type number, with editor completion
In the example above:
- A generic argument explicitly sets the type of
ref. - Function parameters and returned values have type definitions.
- Callers benefit from type checking and editor completion.
❓ Questions and Answers
Q1: How do you declare component event types in Vue 3?
Pass a type declaration with event names and arguments to defineEmits, for example (e: 'click', id: number) => void.
Q2: Why use <script setup lang="ts"> instead of a plain <script>?
<script setup> is compile-time syntax sugar that simplifies code and supports type inference. Combined with TypeScript, it improves the development experience.
Q3: How do you type a composable's return value?
Add type annotations to returned objects or variables, such as const count = ref<number>(0). You can also declare an explicit return type on the function.
🧪 Exercises
Exercise 1
Write a typed counter component that accepts an initial value and an increment step, then emits an update event with the latest value.
<template>
<button @click="increment">Count: {{ count }}</button>
</template>
<script setup lang="ts">
// Write your code here
</script>
Exercise 2
Implement a useToggle composable that accepts an initial boolean value and returns the current state and a toggle function.
// Write your code here
🎉 You now know how to use TypeScript for component type inference and composables in Vue 3, and how type hints support development and debugging! Keep practicing to improve your frontend code.
Template Syntax and New Features
Explore the changes and new features in Vue 3 template syntax and learn the essentials of component development.
Reactivity Internals and Advanced APIs
Understand Vue 3 reactivity, the differences between ref and reactive, and advanced APIs such as toRefs, toRaw, and readonly.
