Performance: Typing computed properties
Typing computed properties affects the runtime efficiency of reactive updates and the speed of recalculations in Vue components.
import { computed, ComputedRef, ref } from 'vue'; const count = ref(0); const double: ComputedRef<number> = computed(() => count.value * 2);
import { computed, ref } from 'vue'; const count = ref(0); const double = computed(() => count.value * 2);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Untyped computed property | May trigger extra reactive updates | Multiple recalculations possible | Increased paint due to unnecessary updates | [X] Bad |
| Explicitly typed computed property | Minimal reactive updates | Single recalculation per dependency change | Paint only when needed | [OK] Good |
fullName.value?
import { ref, computed } from 'vue';
const firstName = ref('John');
const lastName = ref('Doe');
const fullName = computed<string>(() => `${firstName.value} ${lastName.value}`);const count = ref(0); const doubleCount = computed<string>(() => count.value * 2);
items. Which is the correct way to type it in Vue 3 with TypeScript?
const items = ref<string[]>([]); const itemCount = computed<???>(() => items.value.length);
Pass the test and this pattern moves unknown