Vue · Senior · Question 36 of 75
How do you write a generic component (e.g. a typed select)?
<script setup lang="ts" generic="T extends { id: string }">
defineProps<{ items: T[]; modelValue: T | null }>();
defineEmits<{ "update:modelValue": [value: T] }>();
</script>- ✓Correct answer: With the
genericattribute on<script setup> - 2With
defineComponent<T>()only - 3Vue components can't be generic
Explanation
The type parameter is inferred from props at the usage site, so slot props and events are typed for the specific item type.