Performance: Why TypeScript matters in Vue
Using TypeScript in Vue affects development speed and runtime performance by enabling better code quality and fewer runtime errors.
<script setup lang="ts"> import { ref } from 'vue' const count = ref<number>(0) function increment() { count.value += 1 // type-safe increment } </script>
<script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value += '1' // mistake: adding string instead of number } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No TypeScript (loose JS) | Normal | Normal | Normal | [X] Bad - prone to runtime errors |
| With TypeScript (typed Vue) | Normal | Normal | Normal | [OK] Good - fewer runtime errors |
const props = defineProps<{ name: string }>() is the correct TypeScript way to type props in Vue 3's Composition API.props: { name: any } uses any, which loses type safety.const count = ref(0);
function increment() {
count.value += 1;
}
increment();
console.log(count.value);ref(0) creates a reactive number starting at 0. Access with count.value.increment() adds 1 to count.value, so it becomes 1. Then logging count.value outputs 1.const message = ref('Hello');
message = 'Hi';.value property. You cannot assign a string directly to the ref variable itself.message.value = 'Hi', not message = 'Hi'.const props = defineProps<{ value: string | number }>() tells TypeScript the prop can be string or number, enforcing type safety.props: defineProps<{ value: any }>() uses any, which disables type safety.Pass the test and this pattern moves unknown