0
0
Vueframework~20 mins

Typing emits in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Emits Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the emitted event payload type in this Vue component?
Consider this Vue 3 component using defineEmits with TypeScript. What is the type of the payload emitted by the update event?
Vue
const emit = defineEmits<{ (e: 'update', value: number): void }>()

function updateValue() {
  emit('update', 42)
}
AA number value
BAn object with a number property
CA string value
DNo payload
Attempts:
2 left
💡 Hint
Look at the second parameter type in the emit definition.
📝 Syntax
intermediate
2:00remaining
Which option correctly types multiple emits in Vue 3?
You want to type two emits: 'save' with no payload and 'delete' with a string payload. Which defineEmits syntax is correct?
Aconst emit = defineEmits({ save: null, delete: String })
Bconst emit = defineEmits(['save', 'delete'])
Cconst emit = defineEmits<{ save: void; delete: string }>()
Dconst emit = defineEmits<{ (e: 'save'): void; (e: 'delete', id: string): void }>()
Attempts:
2 left
💡 Hint
Use function overload syntax for multiple typed emits.
🔧 Debug
advanced
2:00remaining
Why does this Vue component emit cause a TypeScript error?
This component emits an event 'change' with a string payload, but TypeScript shows an error on emit call. Why?
Vue
const emit = defineEmits<{ (e: 'change', value: number): void }>()

emit('change', 'hello')
AEmits must not have any payload
BThe emitted payload type does not match the defined type
CThe emit function is not called inside setup
DThe event name 'change' is not defined in emits
Attempts:
2 left
💡 Hint
Check the type of the second argument in the emit call.
state_output
advanced
2:00remaining
What is the output when emitting an event with typed emits in Vue 3?
Given this component, what will be logged when trigger is called?
Vue
const emit = defineEmits<{ (e: 'log', message: string): void }>()

function trigger() {
  emit('log', 'Hello Vue')
  console.log('Event emitted')
}

trigger()
ATypeScript error
BNo output
CEvent emitted
DHello Vue
Attempts:
2 left
💡 Hint
Emitting an event does not print anything by itself.
🧠 Conceptual
expert
2:00remaining
Why use defineEmits with TypeScript in Vue 3?
Which is the main benefit of typing emits with defineEmits in Vue 3?
AIt enforces correct event names and payload types at compile time
BIt automatically registers events globally
CIt replaces the need for props in components
DIt enables emitting events without any parameters
Attempts:
2 left
💡 Hint
Think about what TypeScript helps prevent.