Typing emits helps you tell Vue what events your component can send out. This makes your code safer and easier to understand.
Typing emits in Vue
defineEmits<{ eventName: (payload: PayloadType) => void }>()defineEmits inside the <script setup> block.update that sends a string value.const emit = defineEmits<{ update: (value: string) => void }>()
close with no data, and select with a number.const emit = defineEmits<{ close: () => void; select: (id: number) => void }>()
const emit = defineEmits(['open', 'save'])
This Vue component defines a submit event that sends a string. When the button is clicked, it emits the event with the name 'Alice'.
<script setup lang="ts"> const emit = defineEmits<{ submit: (name: string) => void }>() function sendName() { emit('submit', 'Alice') } </script> <template> <button @click="sendName">Send Name</button> </template>
Typing emits helps catch errors if you try to emit an event that is not defined or with wrong data.
You can use both string array or object syntax, but object syntax gives better type safety.
Typing emits tell Vue what events your component can send.
This improves code safety and helps tools give better suggestions.
Use defineEmits inside <script setup> with event names and payload types.
Practice
defineEmits in a Vue component?Solution
Step 1: Understand the role of
defineEmitsdefineEmitsis used inside<script setup>to specify which events a component can send out.Step 2: Identify what it declares
It declares event names and their payload types to improve type safety and tooling support.
Final Answer:
To declare which events the component can emit and their payload types -> Option AQuick Check:
Typing emits = declare events and payloads [OK]
- Confusing defineEmits with state or props
- Thinking it registers plugins
- Using it to import libraries
submit with a payload of type string in Vue 3 <script setup>?Solution
Step 1: Recall the correct typing syntax for emits
Vue 3 usesdefineEmitswith a TypeScript type object describing event names and their payload function signatures.Step 2: Match the correct syntax
The correct syntax isdefineEmits<{ submit: (payload: string) => void }>()which types thesubmitevent with a string payload.Final Answer:
const emit = defineEmits<{ submit: (payload: string) => void }>() -> Option DQuick Check:
Typed emits use object with event function signatures [OK]
- Passing event and type as separate arguments
- Using array with type inside string
- Using object with type directly instead of function signature
<script setup> code:const emit = defineEmits<{ update: (value: number) => void }>();
emit('update', 42);What will happen if you try to emit
update with a string payload like emit('update', 'hello')?Solution
Step 1: Understand the typed emit signature
Theupdateevent expects a payload of typenumberas declared indefineEmits<{ update: (value: number) => void }>().Step 2: Check what happens if payload type mismatches
Passing a string like'hello'violates the type signature, so TypeScript will report a compile-time error before running.Final Answer:
TypeScript will show an error because the payload type is incorrect -> Option BQuick Check:
Typed emits enforce payload types at compile time [OK]
- Thinking Vue throws runtime error instead of TS error
- Assuming event emits anyway without error
- Confusing payload type with event name
<script setup> code snippet:const emit = defineEmits<{ save: (data: object) => void }>();
emit('save');Solution
Step 1: Check the typed emit signature
Thesaveevent expects a payload argument of typeobject.Step 2: Analyze the emit call
The callemit('save')is missing the required payload argument, causing a type error.Final Answer:
Missing payload argument when emitting 'save' event -> Option AQuick Check:
Typed emits require payloads as declared [OK]
- Omitting required payload argument
- Using wrong event name casing
- Thinking type annotations are invalid here
string or number payload with the event name change. Which is the correct way to type this using defineEmits?Solution
Step 1: Understand union types for payloads
To allow multiple types for a payload, use a union type with|, e.g.,string | number.Step 2: Check the options for correct union usage
const emit = defineEmits<{ change: (value: string | number) => void }>() correctly types thechangeevent with a payload that can be eitherstringornumber.Step 3: Identify incorrect options
const emit = defineEmits<{ change: (value: string) => void; change: (value: number) => void }>() repeats the same event name twice, which is invalid. const emit = defineEmits<{ change: (value: string & number) => void }>() uses intersection&which means both types simultaneously (impossible). const emit = defineEmits<{ change: (value: any) => void }>() usesanywhich loses type safety.Final Answer:
const emit = defineEmits<{ change: (value: string | number) => void }>() -> Option CQuick Check:
Use union types for multiple payload types [OK]
- Repeating event names in type object
- Using & instead of | for union types
- Using any which disables type safety
Ready to prove it?
Pass the test and this pattern moves unknown
