Question presented to candidate:
"A teammate's BaseButton emits click and the parent's handler runs twice per click. Another component emits submit with a payload that sometimes has no email. Explain why declaring emits fixes the first bug, how you would validate the second, and what validation can and cannot do."
What a strong answer should cover:
emit()works without declaring, but declaring (emitsoption ordefineEmits) documents the component's API and enables typing.- Declared events are removed from
$attrs. An undeclaredclicklistener falls through to the root element as a native listener, so it fires twice: once fromemit, once from the DOM. - When
emitsis declared, emitting an undeclared event logs a dev warning, which catches typos. - Object syntax adds a validator per event: return
falseand Vue warns. The event is still delivered, and production builds skip the check. - Component events do not bubble; only the direct parent's listener runs.
Clarifying questions expected:
- "Is this
<script setup>? Then I would usedefineEmits, and a type-based declaration in TypeScript."
Code / implementation expected: Yes. A component showing the double-fire problem and an emits object with a validator.