Step 1: Understand ref and value in Vue with TypeScript
ref(0) creates a reactive number starting at 0. Access with count.value.
Step 2: Trace the increment function and console log
Calling increment() adds 1 to count.value, so it becomes 1. Then logging count.value outputs 1.
Final Answer:
1 -> Option A
Quick Check:
Increment ref value once [OK]
Hint: Remember to access ref values with .value [OK]
Common Mistakes:
Logging ref variable without .value
Assuming ref is a plain number
Forgetting to call increment before logging
4. What is the main issue with this Vue + TypeScript code snippet?
const message = ref('Hello');
message = 'Hi';
medium
A. The ref function is missing an import statement.
B. You cannot assign a string directly to a ref variable.
C. The string 'Hi' should be wrapped in ref().
D. The variable message should be declared with let instead of const.
Solution
Step 1: Understand ref assignment rules in Vue with TypeScript
A ref variable holds an object with a .value property. You cannot assign a string directly to the ref variable itself.
Step 2: Identify correct way to update ref value
To change the string inside the ref, assign to message.value = 'Hi', not message = 'Hi'.
Final Answer:
You cannot assign a string directly to a ref variable. -> Option B
Quick Check:
Ref variables require .value for assignment [OK]
Hint: Assign to .value, not the ref variable itself [OK]
Common Mistakes:
Assigning directly to ref variable instead of .value
Confusing ref with normal variables
Ignoring TypeScript error messages
5. You want to create a Vue component with a prop that accepts either a string or number, and you want TypeScript to enforce this. Which is the best way to declare this prop using Vue 3 and TypeScript?
hard
A. props: defineProps<{ value: any }>()
B. props: { value: [String, Number] }
C. const props = defineProps<{ value: string | number }>()