Vue.js course ยท Module 15: TypeScript & Nuxt

Typing Emits

2 min read ยท
In this lesson5

Just as props define the input data of a component, emits define its output events. With TypeScript, we can precisely type what events a component emits and what data each event carries.

defineEmits with Generic Type

<script setup lang="ts">
// Generic syntax
const emit = defineEmits<{
  (e: 'update', value: string): void
  (e: 'delete', id: number): void
  (e: 'close'): void
}>()

// Usage
emit('update', 'new value')  // OK
emit('delete', 42)           // OK
emit('close')                // OK
// emit('update', 123)       // ERROR! Expected string, not number
// emit('unknown')           // ERROR! Unknown event
</script>

New Syntax 3.3+

Since Vue 3.3, a shorter syntax is available:

<script setup lang="ts">
const emit = defineEmits<{
  update: [value: string]
  delete: [id: number]
  close: []
}>()
</script>

Both syntaxes are equivalent. The new one is more concise - we use the event name as the key and arguments as a tuple.

Typed Events in Practice

Here's an example of a station control panel:

<script setup lang="ts">
interface ModuleEvent {
  moduleId: string
  timestamp: number
}

const emit = defineEmits<{
  activate: [event: ModuleEvent]
  deactivate: [event: ModuleEvent]
  alert: [level: 'low' | 'medium' | 'high', message: string]
}>()

function handleActivation(moduleId: string) {
  emit('activate', {
    moduleId,
    timestamp: Date.now()
  })
}

function triggerAlert() {
  emit('alert', 'high', 'Quantum anomaly detected!')
}
</script>

Parent-Child Communication

The child component emits events, the parent listens:

<!-- ControlPanel.vue (child) -->
<script setup lang="ts">
const emit = defineEmits<{
  command: [action: string, target: string]
}>()
</script>

<template>
  <button @click="emit('command', 'scan', 'sector-7')">
    Scan Sector 7
  </button>
</template>
<!-- App.vue (parent) -->
<template>
  <ControlPanel @command="handleCommand" />
</template>

<script setup lang="ts">
function handleCommand(action: string, target: string) {
  console.log(action, target)
  // TypeScript knows the argument types!
}
</script>

Combining Props and Emits

The v-model pattern with types:

<script setup lang="ts">
const props = defineProps<{
  modelValue: string
}>()

const emit = defineEmits<{
  'update:modelValue': [value: string]
}>()

function onInput(event: Event) {
  const target = event.target as HTMLInputElement
  emit('update:modelValue', target.value)
}
</script>

<template>
  <input :value="modelValue" @input="onInput" />
</template>
Code for this lesson: App.vue
<script setup lang="ts">
import { ref } from 'vue'
import CommandPanel from './CommandPanel.vue'

interface CommandLog {
  action: string
  target: string
  timestamp: string
}

const logs = ref<CommandLog[]>([])

function handleCommand(action: string, target: string) {
  logs.value.unshift({
    action,
    target,
    timestamp: new Date().toLocaleTimeString()
  })
}

function handleAlert(level: 'low' | 'medium' | 'high', message: string) {
  logs.value.unshift({
    action: 'ALERT [' + level + ']',
    target: message,
    timestamp: new Date().toLocaleTimeString()
  })
}
</script>

<template>
  <div class="nova-lab">
    <h1>NOVA LAB - Typed Emits</h1>

    <CommandPanel
      @command="handleCommand"
      @alert="handleAlert"
    />

    <div class="log-section">
      <h2>Command log</h2>
      <div v-for="(log, i) in logs" :key="i" class="log-entry">
        <span class="time">{{ log.timestamp }}</span>
        <span class="action">{{ log.action }}</span>
        <span class="target">{{ log.target }}</span>
      </div>
      <p v-if="logs.length === 0" class="empty">No commands</p>
    </div>
  </div>
</template>

<style scoped>
.nova-lab {
  background: #0a0e27;
  color: #00ff88;
  min-height: 100vh;
  padding: 20px;
  font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 15px; }
h2 { color: #00b4d8; margin-top: 20px; }
.log-section { margin-top: 15px; }
.log-entry {
  display: flex;
  gap: 12px;
  padding: 8px 12px;
  margin: 4px 0;
  background: rgba(0,180,216,0.06);
  border-radius: 6px;
  font-size: 13px;
}
.time { color: #666; }
.action { color: #00b4d8; font-weight: bold; }
.target { color: #00ff88; }
.empty { color: #555; font-style: italic; }
</style>

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. Which syntax correctly defines typed emits in Vue 3.3+?

  2. 2. Given const emit = defineEmits<{ alert: [level: string, msg: string] }>(), which call is correct?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Click in order

    Arrange the steps for implementing a typed v-model in a component:

  • Vertical ordering

    Arrange the data flow in typed parent-child communication:

  1. QuestionGeneric defineEmits Syntax
  2. QuestionCalling a Typed Emit
  3. Click in orderv-model Pattern with Types
  4. Put in orderComponent Communication with Types
  5. QuestionLegacy Emits Syntax

Useful articles