tag?
import { ref, computed } from 'vue'; export default { setup() { const count = ref(5); const doubleCount = computed(() => count.value * 2); return { count, doubleCount }; } }; // Template: // <p>{{ doubleCount }}</p>
The computed property doubleCount multiplies count.value (which is 5) by 2, so it returns 10. The type number ensures the computed value is a number.
Option B correctly types the return value of the function as string. Option B is invalid because computed does not accept a generic type parameter directly. Option B uses a type assertion inside the function but does not type the computed itself. Option B returns a number but claims string, causing a type mismatch.
import { ref, computed } from 'vue'; const count = ref(3); const message = computed((): string => { if (count.value > 5) return 'High'; // Missing return in else case });
The computed function must return a string in every possible path. Here, if count.value <= 5, the function returns nothing (undefined), causing a type error.
fullName after updating firstName?fullName.value be after firstName.value = 'Jane'?import { ref, computed } from 'vue'; const firstName = ref('John'); const lastName = ref('Doe'); const fullName = computed(() => `${firstName.value} ${lastName.value}`); firstName.value = 'Jane';
When firstName.value changes to 'Jane', the computed fullName updates reactively to 'Jane Doe'.
Vue's computed function infers the return type from the function passed in. Typing the function's return type is the recommended and cleanest way to type computed properties. Providing a generic parameter directly to computed is not supported. Casting manually is unnecessary if the function is typed properly.