0
0
Vueframework~20 mins

Typing ref and reactive in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Reactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Vue component with typed ref?

Consider this Vue 3 component using ref with TypeScript typing. What will be rendered inside the paragraph?

Vue
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const count = ref<number>(5);
    count.value += 3;
    return { count };
  },
  template: `<p>{{ count }}</p>`
});
A<p>NaN</p>
B<p>8</p>
C<p>undefined</p>
D<p>5</p>
Attempts:
2 left
💡 Hint

Remember that ref wraps the value and you access it with .value.

📝 Syntax
intermediate
2:00remaining
Which option correctly types a reactive object in Vue 3 with TypeScript?

Given this reactive object, which option correctly types it to have name as string and age as number?

Vue
import { reactive } from 'vue';

const person = reactive( /* ??? */ );
A{ name: 'Alice', age: '30' } as { name: string; age: number }
Breactive<{ name: string; age: number }>({ name: 'Alice', age: 30 })
Creactive({ name: 'Alice', age: '30' })
D{ name: 'Alice', age: 30 } as { name: string; age: number }
Attempts:
2 left
💡 Hint

Type assertion is done outside the reactive call in Vue 3.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 code cause a TypeScript error?

Look at this code snippet. Why does TypeScript show an error on state.count = 'ten'?

Vue
import { reactive } from 'vue';

const state = reactive({ count: 0 });

state.count = 'ten';
ABecause <code>count</code> is typed as number and assigning a string is invalid
BBecause <code>reactive</code> only accepts strings as values
CBecause <code>state</code> is readonly and cannot be changed
DBecause <code>count</code> must be accessed with <code>.value</code>
Attempts:
2 left
💡 Hint

Check the initial type of count in the reactive object.

state_output
advanced
2:00remaining
What is the value of message.value after this code runs?

Given this Vue 3 setup, what is the final value of message.value?

Vue
import { ref } from 'vue';

const message = ref<string | null>(null);

if (!message.value) {
  message.value = 'Hello';
}

message.value += ' World!';
A'Hello World!'
Bnull
C'null World!'
Dundefined
Attempts:
2 left
💡 Hint

Remember how null and string concatenation work in JavaScript.

🧠 Conceptual
expert
3:00remaining
Which statement about typing ref and reactive in Vue 3 is TRUE?

Choose the correct statement about how TypeScript typing works with ref and reactive in Vue 3.

A<code>reactive</code> objects always require generic type parameters to work correctly
B<code>ref</code> values are accessed directly without <code>.value</code> in TypeScript
C<code>ref</code> requires explicit type only when the initial value is <code>null</code> or <code>undefined</code>
D<code>reactive</code> returns a readonly object that cannot be mutated
Attempts:
2 left
💡 Hint

Think about when TypeScript needs help to infer types for refs.