Consider this Vue 3 component using ref with TypeScript typing. What will be rendered inside the paragraph?
import { defineComponent, ref } from 'vue'; export default defineComponent({ setup() { const count = ref<number>(5); count.value += 3; return { count }; }, template: `<p>{{ count }}</p>` });
Remember that ref wraps the value and you access it with .value.
The count ref starts at 5, then 3 is added to count.value, making it 8. The template renders count which unwraps the ref automatically, so it shows 8.
Given this reactive object, which option correctly types it to have name as string and age as number?
import { reactive } from 'vue'; const person = reactive( /* ??? */ );
Type assertion is done outside the reactive call in Vue 3.
Option D correctly asserts the type of the object before passing it to reactive. Option D incorrectly uses a generic parameter with reactive, which is not supported. Options C and D have age as string, which conflicts with the number type.
Look at this code snippet. Why does TypeScript show an error on state.count = 'ten'?
import { reactive } from 'vue'; const state = reactive({ count: 0 }); state.count = 'ten';
Check the initial type of count in the reactive object.
The reactive object infers count as a number. Assigning a string to it violates the type, causing a TypeScript error. Option A is false because reactive accepts any type. Option A is false because reactive objects are mutable. Option A is false because .value is for refs, not reactive objects.
message.value after this code runs?Given this Vue 3 setup, what is the final value of message.value?
import { ref } from 'vue'; const message = ref<string | null>(null); if (!message.value) { message.value = 'Hello'; } message.value += ' World!';
Remember how null and string concatenation work in JavaScript.
Initially, message.value is null. The if block sets it to 'Hello'. Then ' World!' is appended, resulting in 'Hello World!'.
ref and reactive in Vue 3 is TRUE?Choose the correct statement about how TypeScript typing works with ref and reactive in Vue 3.
Think about when TypeScript needs help to infer types for refs.
Option C is true: when ref is initialized with null or undefined, you must specify the type to avoid any. Option C is false because reactive infers types from the object passed. Option C is false because ref values must be accessed with .value. Option C is false because reactive returns a mutable proxy.