Question presented to candidate: "We are moving a Vue 3 app to TypeScript. Show me how you would type a component's props and emits, refs and template refs, and provide/inject, and tell me how type errors in templates get caught before they reach production."
What a strong answer should cover:
<script setup lang="ts">with type-baseddefineProps<{...}>(); the compiler generates the runtime props declaration from the type. Defaults via reactive props destructure (Vue 3.5) orwithDefaults.- Type-based
defineEmitswith the named-tuple syntax (change: [id: number], Vue 3.3+), soemitcalls and parent handlers are checked. ref<T>()for unions or nullable values, inferred otherwise;useTemplateRef(Vue 3.5) for DOM and component refs, typed from the template or explicitly.InjectionKey<T>for typedprovide/inject, rememberinginject()may returnundefined.vue-tsctype-checks.vuefiles including templates; plaintsccannot import.vue, andvite buildstrips types without checking, so CI must runvue-tsc --noEmit(create-vue wires it astype-check).- Types are compile-time only: Vue still validates props at runtime in development from the generated declaration.
Clarifying questions expected:
- "Which Vue version are we on? Props destructure,
useTemplateRefand the tuple emits syntax depend on it." - "Is the build already running
vue-tsc, or only Vite?"
Code / implementation expected: Yes β a typed child with props and emits, a parent using it, and at least one real vue-tsc error.