Question presented to candidate:
"In our <script setup> components I see defineProps and defineEmits used without any import. Where do they come from, what do they turn into, and what are the rules and limits when you use them with TypeScript?"
What a strong answer should cover:
- They are compiler macros: the SFC compiler removes the calls and writes
propsandemitsoptions into the component; there is nothing to import (importing triggers a warning). definePropsreturns the props object;defineEmitsreturns a typedemitfunction.- Two forms: runtime (
defineProps({ title: String })) and type-based (defineProps<{ title: string }>()), which the compiler converts into runtime options. You cannot use both in one call. - Defaults for type-based props:
withDefaults(), or since Vue 3.5 reactive props destructure (const { likes = 0 } = defineProps<...>()), with the getter rule forwatch. - The arguments are hoisted out of
setup(), so they cannot reference local variables. vue-tscchecks the parent's usage against these types.
Clarifying questions expected:
- "Is the project on Vue 3.5 or later? That decides between
withDefaultsand destructured defaults."
Code / implementation expected: Yes. A typed component using both macros, ideally with the compiled output.