Hint: Access ref values with .value to get or set [OK]
Common Mistakes:
Logging ref directly without .value
Confusing reactive object access with ref
Expecting initial value after update
4. What is wrong with this Vue 3 code snippet?
const state = reactive<{ count: number }>({ count: 0 });
state.count = 'one';
medium
A. Runtime error: reactive objects are immutable
B. Syntax error: reactive cannot be typed
C. No error, code works fine
D. Type error: assigning string to number property
Solution
Step 1: Check the declared type of reactive object
The object is typed as { count: number }, so count must be a number.
Step 2: Analyze the assignment
Assigning 'one' (a string) to count violates the type, causing a type error.
Final Answer:
Type error: assigning string to number property -> Option D
Quick Check:
Type mismatch in reactive object property = C [OK]
Hint: Check property types before assigning new values [OK]
Common Mistakes:
Ignoring type errors in reactive objects
Thinking reactive objects are immutable
Confusing syntax errors with type errors
5. You want to create a reactive state in Vue 3 that holds a list of user objects with typed properties id: number and name: string. Which is the best way to type and initialize this state?
hard
A. const users = ref<Array<{ id: number; name: string }>>([])
B. const users = reactive<Array<{ id: number; name: string }>>({})
C. const users = reactive<{ id: number; name: string }>([])
D. const users = ref<{ id: number; name: string }>([])
Solution
Step 1: Decide between ref and reactive for arrays
Arrays are objects, but Vue recommends using ref for arrays to keep reactivity simple and clear.
Step 2: Apply correct typing for array of objects
Typing as Array<{ id: number; name: string }> ensures each user object has correct properties.
Step 3: Check initialization syntax
ref<Array<{ id: number; name: string }>>([]) initializes an empty reactive array with proper typing.