Bird
Raised Fist0
Vueframework~20 mins

Generic components in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Generic Components Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the rendered output of this generic Vue component?

Consider this Vue 3 generic component using the Composition API:

<script setup lang="ts">
import { defineProps } from 'vue'

type Item = { id: number; name: string }

const props = defineProps<{ items: Item[] }>()
</script>

<template>
  <ul>
    <li v-for="item in props.items" :key="item.id">{{ item.name }}</li>
  </ul>
</template>

If you pass items=[{id:1, name:'Apple'}, {id:2, name:'Banana'}], what will be the output?

Vue
<script setup lang="ts">
import { defineProps } from 'vue'

type Item = { id: number; name: string }

const props = defineProps<{ items: Item[] }>()
</script>

<template>
  <ul>
    <li v-for="item in props.items" :key="item.id">{{ item.name }}</li>
  </ul>
</template>
A<ul><li>props.items</li></ul>
B<ul><li>1</li><li>2</li></ul>
C<ul><li>Apple</li><li>Banana</li></ul>
D<ul><li>{id:1, name:'Apple'}</li><li>{id:2, name:'Banana'}</li></ul>
Attempts:
2 left
💡 Hint

Look at what is displayed inside the <li> tags.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a generic Vue component prop type?

In Vue 3 with TypeScript, you want to define a generic component that accepts a prop data of type T[]. Which syntax is correct?

Aconst props = defineProps<{ data: T[] }>()
Bconst props = defineProps<{ data: Array<T> }>()
Cconst props = defineProps<T>()
Dconst props = defineProps<{ data: T }>()
Attempts:
2 left
💡 Hint

Remember that Array<T> and T[] are equivalent in TypeScript.

🔧 Debug
advanced
2:00remaining
Why does this generic Vue component fail to compile?

Given this Vue 3 component code snippet:

<script setup lang="ts">
import { defineProps } from 'vue'

type Item<T> = { id: number; value: T }

const props = defineProps<{ items: Item[] }>()
</script>

Why does it fail?

Vue
<script setup lang="ts">
import { defineProps } from 'vue'

type Item<T> = { id: number; value: T }

const props = defineProps<{ items: Item[] }>()
</script>
ABecause Item is a generic type but used without specifying T
BBecause defineProps cannot accept generic types
CBecause id must be a string, not number
DBecause value property is missing in props
Attempts:
2 left
💡 Hint

Check how generic types are used in TypeScript.

❓ state_output
advanced
2:00remaining
What is the value of count after clicking the button twice in this generic Vue component?

Consider this Vue 3 generic component:

<script setup lang="ts">
import { ref } from 'vue'

const count = ref(0)

function increment() {
  count.value++
}
</script>

<template>
  <button @click="increment">Increment</button>
  <p>Count: {{ count.value }}</p>
</template>

What will be displayed after clicking the button twice?

Vue
<script setup lang="ts">
import { ref } from 'vue'

const count = ref(0)

function increment() {
  count.value++
}
</script>

<template>
  <button @click="increment">Increment</button>
  <p>Count: {{ count.value }}</p>
</template>
ACount: 2
BCount: 1
CCount: 0
DCount: undefined
Attempts:
2 left
💡 Hint

Each click increases count by 1.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue 3 generic components is true?

Choose the correct statement about using generic components in Vue 3 with TypeScript.

AVue automatically infers generic types from prop values without any TypeScript annotations
BYou must explicitly specify generic type parameters when using generic components in templates
CGeneric components can only be defined using the Options API, not Composition API
DGeneric types in Vue props help ensure type safety but do not affect runtime behavior
Attempts:
2 left
💡 Hint

Think about TypeScript's role in Vue components.

Practice

(1/5)
1. What is the main benefit of using generic components in Vue?
easy
A. They allow reusing one component for different data types.
B. They automatically style components without CSS.
C. They replace the need for Vue Router.
D. They make components load faster by default.

Solution

  1. Step 1: Understand generic components purpose

    Generic components are designed to be reusable with different data types, so you don't rewrite similar components for each type.
  2. Step 2: Compare options to this purpose

    Options B, C, and D describe unrelated features. Only They allow reusing one component for different data types. matches the core benefit of generic components.
  3. Final Answer:

    They allow reusing one component for different data types. -> Option A
  4. Quick Check:

    Generic components = reuse for many data types [OK]
Hint: Generic means reusable for many types, not styling or routing [OK]
Common Mistakes:
  • Confusing generic components with styling features
  • Thinking generic components handle routing
  • Assuming generic components improve load speed automatically
2. Which syntax correctly defines props with a generic type in a Vue 3 component using TypeScript?
easy
A. const props = defineProps(['item']);
B. const props = defineProps<{ item: T }>();
C. const props = defineProps<T>();
D. const props = defineProps({ item: String });

Solution

  1. Step 1: Recall TypeScript generic prop syntax in Vue

    Using defineProps with a generic type requires specifying the prop shape with a type parameter, like { item: T }.
  2. Step 2: Analyze each option

    const props = defineProps<{ item: T }>(); correctly uses defineProps<{ item: T }>(). const props = defineProps<T>(); misses the prop shape object. const props = defineProps(['item']); uses string array syntax, not generic. const props = defineProps({ item: String }); uses a fixed type, not generic.
  3. Final Answer:

    const props = defineProps<{ item: T }>(); -> Option B
  4. Quick Check:

    Generic props need object shape with type parameter [OK]
Hint: Generic props need object shape with type parameter in defineProps [OK]
Common Mistakes:
  • Using array syntax instead of type object for generic props
  • Omitting the prop shape object in defineProps
  • Using fixed types instead of generics
3. Given this generic Vue component snippet, what will be the rendered output if item is a string "Hello"?
const props = defineProps<{ item: T }>();
const item = props.item;
return <div>{typeof item}: {item}</div>;
medium
A. Runtime error due to missing type
B. <div>object: Hello</div>
C. <div>number: Hello</div>
D. <div>string: Hello</div>

Solution

  1. Step 1: Understand the typeof operator on the prop

    The typeof operator returns the type of the value at runtime. Since item is "Hello", a string, typeof item is "string".
  2. Step 2: Determine the rendered output

    The returned JSX renders a div with text: "string: Hello".
  3. Final Answer:

    <div>string: Hello</div> -> Option D
  4. Quick Check:

    typeof 'Hello' = string, so output matches [OK]
Hint: typeof a string value returns 'string' in output [OK]
Common Mistakes:
  • Confusing typeof result with the value itself
  • Expecting object or number type for string value
  • Assuming runtime error without type issues
4. Identify the error in this generic Vue component code:
const props = defineProps<{ item: T }>();
const item = props.item;
console.log(item.value);
medium
A. Accessing value assumes item is a ref, but it may not be.
B. Missing import of defineProps causes syntax error.
C. Generic type T must be declared before use.
D. Props must be defined as an array, not an object.

Solution

  1. Step 1: Analyze usage of item.value

    Accessing value is valid only if item is a Vue ref. The generic type T could be any type, so item might not have value.
  2. Step 2: Check other options

    Missing import of defineProps causes syntax error. is incorrect because defineProps is globally available in Vue SFCs. Generic type T must be declared before use. is incorrect because T is assumed declared in the generic context. Props must be defined as an array, not an object. is wrong because props can be defined as an object type.
  3. Final Answer:

    Accessing value assumes item is a ref, but it may not be. -> Option A
  4. Quick Check:

    Accessing .value requires ref type, generic may not be ref [OK]
Hint: Only refs have .value; generics may be plain values [OK]
Common Mistakes:
  • Assuming generic type is always a ref
  • Thinking defineProps needs import in SFC
  • Believing props must be array, not object
5. You want to create a generic Vue component that accepts a list of items of any type and renders each item with a slot. Which approach correctly applies generics and slots together?
hard
A. Use defineProps(['items']) and render items directly without slots.
B. Use defineProps<{ items: any[] }>() and hardcode item rendering inside component.
C. Use defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop.
D. Use defineProps<{ items: T }>() and render items without looping.

Solution

  1. Step 1: Define generic props for list of items

    Using defineProps<{ items: T[] }>() correctly types the prop as an array of generic type T.
  2. Step 2: Use slots with v-for to render each item

    Passing each item as a slot prop allows flexible rendering by the parent component, which is the correct pattern for generic components with slots.
  3. Step 3: Evaluate other options

    Use defineProps(['items']) and render items directly without slots. lacks generics and slots. Use defineProps<{ items: any[] }>() and hardcode item rendering inside component. uses any losing type safety and hardcodes rendering. Use defineProps<{ items: T }>() and render items without looping. uses T instead of T[] and misses looping.
  4. Final Answer:

    Use defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop. -> Option C
  5. Quick Check:

    Generic list + slot with v-for = Use defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop. [OK]
Hint: Generic list props need array type and slot with v-for [OK]
Common Mistakes:
  • Using single item type instead of array for list props
  • Not using slots for flexible item rendering
  • Hardcoding rendering inside generic component
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard