0
0
Vueframework~20 mins

Typing composables in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Typing Composables 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 composable usage?

Consider this Vue 3 composable using TypeScript. What will be the console output when the component using it is mounted?

Vue
import { ref } from 'vue';

function useCounter() {
  const count = ref<number>(0);
  function increment() {
    count.value++;
  }
  return { count, increment };
}

// In component setup
const { count, increment } = useCounter();
increment();
increment();
console.log(count.value);
A2
B0
Cundefined
DNaN
Attempts:
2 left
💡 Hint

Remember that ref creates a reactive value with a .value property.

📝 Syntax
intermediate
2:00remaining
Which option correctly types a composable returning a reactive object?

Which of the following composable function signatures correctly types a reactive object with a message string property in Vue 3 with TypeScript?

Afunction useMessage(): { message: string } { const message = ref('hello'); return { message }; }
Bfunction useMessage(): { message: Ref<string> } { const message = ref('hello'); return { message }; }
Cfunction useMessage(): Ref<string> { const message = ref('hello'); return message; }
Dfunction useMessage(): { message: Reactive<string> } { const message = ref('hello'); return { message }; }
Attempts:
2 left
💡 Hint

Remember that ref returns a Ref type wrapping the value.

🔧 Debug
advanced
2:00remaining
What error does this typed composable cause?

Given this composable, what error will TypeScript report?

Vue
import { ref } from 'vue';

function useFlag() {
  const flag = ref<boolean>(false);
  function toggle() {
    flag.value = 'true';
  }
  return { flag, toggle };
}
AType 'string' is not assignable to type 'boolean'.
BProperty 'value' does not exist on type 'boolean'.
CCannot redeclare block-scoped variable 'flag'.
DNo error, code is valid.
Attempts:
2 left
💡 Hint

Check the type assigned to flag.value inside toggle.

state_output
advanced
2:00remaining
What is the value of fullName after calling updateName?

Analyze this typed composable and determine the value of fullName.value after updateName() is called.

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

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

  function updateName() {
    firstName.value = 'Jane';
    lastName.value = 'Smith';
  }

  return { fullName, updateName };
}

const { fullName, updateName } = useUser();
updateName();
A"John Smith"
B"John Doe"
C"Jane Doe"
D"Jane Smith"
Attempts:
2 left
💡 Hint

Remember that computed updates reactively when dependencies change.

🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of typing composables in Vue 3 with TypeScript?

Why is it important to add explicit TypeScript types to Vue 3 composables?

ATo prevent Vue from tracking reactivity and improve performance by disabling proxies.
BTo make the composable run faster in the browser by optimizing JavaScript execution.
CTo ensure the composable's reactive state and functions have clear, predictable types, improving code safety and editor support.
DTo automatically generate UI elements based on the composable's types without writing templates.
Attempts:
2 left
💡 Hint

Think about benefits of TypeScript in code quality and developer experience.