Vue3 Fundamentals · Vue 3 + TypeScript

Generic components

Generic components

A list component should work for any kind of item, not only the one you wrote it for. In a <script setup lang="ts"> block you make the component generic by adding generic="T" and using T in the props.

<script setup lang="ts" generic="T">
defineProps<{ items: T[] }>();
</script>

<template>
  <ul>
    <li v-for="(item, index) in items" :key="index">{{ item }}</li>
  </ul>
</template>

generic="T" declares a type parameter for the component. items: T[] says the prop is an array of whatever T is, and Vue infers T from the array the parent passes. Pass strings and T is string; pass objects and T is that object.

The type parameter is not a runtime value — it is erased when the component compiles. Its job is to keep the parent and the child agreeing on the item type, so the editor checks each item where you render it.

In App.vue, make the list generic over its item type.

Challenge

The list is not generic and does not render its items. Export an App component from App.vue that declares generic="T", takes an items: T[] prop, and renders one <li> per item.

The Test mounts your component with items: ['Ada', 'Grace'] and reads the list items. The starter joins the items into a single paragraph, so the Test finds no list items and fails.

App.vue
<script setup lang="ts" generic="T">
defineProps<{ items: T[] }>();
</script>

<template>
  <p>{{ items.join(', ') }}</p>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.