Question presented to candidate:
"Our codebase has dozens of components with a modelValue prop, an update:modelValue emit and a writable computed in between. A colleague wants to replace them all with defineModel(). What does defineModel actually generate, what behaves differently from the manual version, and is there anything that could bite us during the migration?"
What a strong answer should cover:
defineModel()is a compiler macro (stable since Vue 3.4, experimental in 3.3) that declares themodelValueprop, amodelModifiersprop and theupdate:modelValueemit, and returns a ref built withuseModel().- Reading the ref gives the prop; assigning it emits the update event. Named models:
defineModel('title'). - If the parent passes no
v-model, the ref works as local state (the manual version cannot do that without extra code). - When the parent does bind it, the ref only changes after the parent re-renders, so a read right after assignment returns the old value.
- A
defaultthat the parent does not have leaves parent and child out of sync until the first emit. - Options (
type,required,default) go to the prop;get/settransform values, which is how custom modifiers are implemented.
Clarifying questions expected:
- "Do any of these components rely on reading the new value immediately after setting it?"
- "Are they TypeScript components? Then
defineModel<string>()gives the prop type."
Code / implementation expected: Yes. A component using defineModel, and ideally the compiled output or the useModel equivalent.