Consider this Vue 3 composable using TypeScript. What will be the console output when the component using it is mounted?
import { ref } from 'vue'; function useCounter() { const count = ref<number>(0); function increment() { count.value++; } return { count, increment }; } // In component setup const { count, increment } = useCounter(); increment(); increment(); console.log(count.value);
Remember that ref creates a reactive value with a .value property.
The count ref starts at 0. Each call to increment() increases count.value by 1. After two increments, count.value is 2.
Which of the following composable function signatures correctly types a reactive object with a message string property in Vue 3 with TypeScript?
Remember that ref returns a Ref type wrapping the value.
Option B correctly types the returned object with message as a Ref<string>. Option B incorrectly types message as a string, but it is a Ref. Option B returns the Ref directly, not an object. Option B uses a non-existent Reactive type.
Given this composable, what error will TypeScript report?
import { ref } from 'vue'; function useFlag() { const flag = ref<boolean>(false); function toggle() { flag.value = 'true'; } return { flag, toggle }; }
Check the type assigned to flag.value inside toggle.
The flag ref is typed as boolean. Assigning a string 'true' to flag.value causes a type error: Type 'string' is not assignable to type 'boolean'.
fullName after calling updateName?Analyze this typed composable and determine the value of fullName.value after updateName() is called.
import { ref, computed } from 'vue'; function useUser() { const firstName = ref<string>('John'); const lastName = ref<string>('Doe'); const fullName = computed(() => `${firstName.value} ${lastName.value}`); function updateName() { firstName.value = 'Jane'; lastName.value = 'Smith'; } return { fullName, updateName }; } const { fullName, updateName } = useUser(); updateName();
Remember that computed updates reactively when dependencies change.
After calling updateName(), firstName.value is 'Jane' and lastName.value is 'Smith'. The computed fullName reflects these changes, so its value is 'Jane Smith'.
Why is it important to add explicit TypeScript types to Vue 3 composables?
Think about benefits of TypeScript in code quality and developer experience.
Typing composables helps developers catch errors early, understand the data shapes, and get better autocomplete and documentation in editors. It does not affect runtime speed or UI generation automatically.