0
0
Vueframework~20 mins

Typing computed properties in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Computed Properties 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 typed computed property?
Consider this Vue 3 component using the Composition API. What will be the rendered output inside the

tag?

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

export default {
  setup() {
    const count = ref(5);
    const doubleCount = computed(() => count.value * 2);
    return { count, doubleCount };
  }
};

// Template:
// <p>{{ doubleCount }}</p>
Aundefined
B5
C10
DNaN
Attempts:
2 left
💡 Hint
Remember computed properties return the value of the function inside them.
📝 Syntax
intermediate
2:00remaining
Which option correctly types a computed property returning a string?
You want to create a computed property that returns a string greeting. Which code snippet correctly types it in Vue 3 Composition API?
Aconst greeting = computed<string>(() => `Hello, Vue!`);
Bconst greeting = computed((): string => `Hello, Vue!`);
Cconst greeting = computed(() => `Hello, Vue!` as string);
Dconst greeting = computed<string>(() => { return 123; });
Attempts:
2 left
💡 Hint
Typing the function return is the recommended way.
🔧 Debug
advanced
2:00remaining
Why does this typed computed property cause a TypeScript error?
Examine the code below. Why does TypeScript report an error on the computed property?
Vue
import { ref, computed } from 'vue';

const count = ref(3);
const message = computed((): string => {
  if (count.value > 5) return 'High';
  // Missing return in else case
});
ABecause the computed function does not return a string in all code paths.
BBecause computed cannot be typed with <string> generic.
CBecause ref must be typed explicitly when used with computed.
DBecause count.value is not reactive inside computed.
Attempts:
2 left
💡 Hint
Check if all possible paths return the expected type.
state_output
advanced
2:00remaining
What is the value of fullName after updating firstName?
Given this Vue 3 component, what will fullName.value be after firstName.value = 'Jane'?
Vue
import { ref, computed } from 'vue';

const firstName = ref('John');
const lastName = ref('Doe');
const fullName = computed(() => `${firstName.value} ${lastName.value}`);

firstName.value = 'Jane';
A"Jane Doe"
B"John Doe"
C"Jane John"
D"Doe Jane"
Attempts:
2 left
💡 Hint
Computed properties update automatically when dependencies change.
🧠 Conceptual
expert
2:00remaining
Which statement about typing computed properties in Vue 3 is correct?
Select the one true statement about typing computed properties in Vue 3 Composition API with TypeScript.
AYou should cast the computed property to <code>ComputedRef&lt;T&gt;</code> manually after creation.
BComputed properties cannot be typed and always infer <code>any</code> type.
CYou must always provide a generic type parameter to <code>computed</code> to type its return value.
DTyping the return type of the function passed to <code>computed</code> is the recommended way to ensure correct typing.
Attempts:
2 left
💡 Hint
Think about how TypeScript infers types from functions.