Prop Types and Defaults
Part of the Fundamentals section of Coddy's Vue journey. Lesson 34 of 55.
Runtime prop declarations can document expected types and supply defaults. An omitted capacity uses 6; an explicit numeric zero stays zero.
defineProps({
title: { type: String, default: 'Workshop' },
capacity: { type: Number, default: 6 }
});Defaults apply when a prop is undefined, including when it is omitted. A type declaration describes the expected value and can warn during development; it is not a substitute for validating external data. Use bound expressions to pass numbers instead of assuming a type declaration converts strings.
Challenge
MediumCreate SessionCard.vue with runtime prop declarations: label is a string defaulting to Open session, and seats is a number defaulting to 4. Show the label in h2 and seats plus one as N including host in a paragraph. Render a default card in #default and a custom card in #custom with label Mending and numeric seats 8.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
7Reusable Components
Importing ComponentsIndependent InstancesPassing PropsProp Types and DefaultsRecap: Component Cards2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks