Question presented to candidate:
"We have a DataTable component used for users, orders and products. Today its rows prop is any[], so the row slot and the select event lose all typing. How would you make it generic in <script setup>, and what does that actually change at runtime?"
What a strong answer should cover:
<script setup lang="ts" generic="T">declares a type parameter for the whole component, with constraints and multiple parameters allowed (generic="T extends { id: string }, K extends keyof T").Tcan be used indefineProps,defineEmits,defineModel,defineSlotsand local code, and is inferred from the props the parent passes.- The parent then gets typed slot props, event payloads and
v-modelwith no annotations. - It is type-only: the compiled component has runtime props such as
{ type: Array }and no knowledge ofT, so checking happens in the IDE andvue-tsc, not in the browser. - Constraint violations and wrong usage surface as real
vue-tscerrors; plaintscandvite builddo not check templates. - Introduced in Vue 3.3; the non-SFC equivalent is a generic
defineComponentsetup function.
Clarifying questions expected:
- "Do rows always share a key field? That becomes the generic constraint."
- "Is
vue-tscpart of CI? Without it the generic only helps in the editor."
Code / implementation expected: Yes β a generic list with a typed slot and event, and a parent that triggers at least one type error.