defineProps and defineEmits
Typed props with defineProps (), defineEmits (), withDefaults() for defaults.
defineProps and defineEmits is a free Vue Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Communicating Between Components
Components receive data through props and send messages up through events. In <script setup> you declare these with the defineProps and defineEmits macros.
defineProps the Runtime Way
You can declare props with a runtime object, just like the Options API. This works in both JS and TS.
<script setup>
const props = defineProps({
title: String,
count: Number
})
console.log(props.title)
</script>defineProps with a TypeScript Generic
In TypeScript, pass a type via the generic for cleaner, type-checked props. The compiler turns the type into runtime declarations.
<script setup lang="ts">
const props = defineProps<{
title: string
count: number
}>()
</script>Optional Props with ?
Add a question mark to a property in the type to make it optional. Optional props may be undefined if the parent does not pass them.
<script setup lang="ts">
const props = defineProps<{
title: string
subtitle?: string
}>()
</script>Default Values with withDefaults
The type-based form has no place for defaults, so Vue provides withDefaults. Wrap defineProps and supply a defaults object.
<script setup lang="ts">
const props = withDefaults(defineProps<{
title?: string
count?: number
}>(), {
title: 'Untitled',
count: 0
})
</script>Using Props in the Template
Props are reactive. Reference them directly in the template, or through props.x in script.
<template>
<h1>{{ title }}</h1>
<p>Count: {{ count }}</p>
</template>Props Are Read-Only
Never mutate a prop inside the child. Props flow one way: parent to child. If you need a local editable copy, derive it with computed or a local ref.
Declaring Events with defineEmits
defineEmits declares which events a component can fire. The runtime form takes an array of event names.
<script setup>
const emit = defineEmits(['submit', 'cancel'])
function onSave() { emit('submit') }
</script>Typed Events with TypeScript
The type-based form lets you specify event names and payload types. This catches mistakes like emitting the wrong argument type.
<script setup lang="ts">
const emit = defineEmits<{
(e: 'submit', payload: string): void
(e: 'cancel'): void
}>()
</script>Emitting with a Payload
Call emit with the event name and any data. The parent listens with @submit and receives the payload.
<script setup lang="ts">
const emit = defineEmits<{ (e: 'submit', value: string): void }>()
function send() { emit('submit', 'hello') }
</script>Putting It Together
A child receives a title prop and emits a submit event. The parent reacts to both — clean, typed, one-way data plus explicit events.
<script setup lang="ts">
defineProps<{ title: string }>()
const emit = defineEmits<{ (e: 'submit', v: string): void }>()
</script>Quick Check
Check your grasp of props and emits.
Recap
You learned component I/O in <script setup>:
definePropsdeclares inputs (runtime object or TS generic).- Use
?for optional props andwithDefaultsfor defaults. - Props are read-only and one-way.
defineEmitsdeclares events, with optional typed names and payloads.
Frequently asked questions
Is the “defineProps and defineEmits” lesson free?
Yes — the full text of “defineProps and defineEmits” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “defineProps and defineEmits”?
Typed props with defineProps (), defineEmits (), withDefaults() for defaults. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “defineProps and defineEmits” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to script setup
- defineProps and defineEmits
- defineExpose and useAttrs
- Importing and Using Composables in script setup