Default Values in Destructuring
Combine defaults with type annotations safely.
Default Values in Destructuring is a free TypeScript Academy lesson on CoddyKit — lesson 3 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 TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Filling in the Blanks
Destructuring defaults supply a fallback when a property or element is missing or undefined. Combined with TypeScript's inference, they make optional configuration ergonomic and safe.
A Default in Object Destructuring
Write = value after a binding to give it a fallback. If the property is absent or undefined, the default is used instead.
const opts: { count?: number } = {}
const { count = 0 } = opts
console.log(count) // 0Defaults With Optional Properties
Defaults pair naturally with optional (?) properties. The default removes the undefined case, so the resulting variable is the non-optional type.
interface Opts { retries?: number }
function connect(opts: Opts) {
const { retries = 3 } = opts
// retries is number, not number | undefined
return retries * 2
}
console.log(connect({}))Defaults and Inferred Types
If you do not annotate, TypeScript infers the variable's type from the default value. Here 0 makes count a number.
const settings: { count?: number; label?: string } = { label: "hi" }
const { count = 0, label = "none" } = settings
console.log(count, label) // 0 hiOnly undefined Triggers the Default
A subtle point: defaults apply only for undefined, never for other falsy values like 0, "", or null. Those pass through unchanged.
const { x = 10 } = { x: 0 }
console.log(x) // 0, not 10 — 0 is not undefined
const { y = 10 } = { y: undefined as number | undefined }
console.log(y) // 10Defaults in Function Parameters
Destructured parameter defaults make optional options painless. Each field can have its own fallback right in the signature.
function draw({ x = 0, y = 0 }: { x?: number; y?: number }) {
return x + "," + y
}
console.log(draw({ x: 5 })) // 5,0Defaulting the Whole Parameter
To let callers omit the argument object entirely, give the parameter itself a default of {}. Without this, calling with no argument would throw.
function draw({ x = 0, y = 0 }: { x?: number; y?: number } = {}) {
return x + "," + y
}
console.log(draw()) // 0,0Defaults With Renaming
You can rename and default in one binding: rename first, then add the default after the new name.
const { count: total = 0 } = { } as { count?: number }
console.log(total) // 0Computed Defaults
A default can be any expression, even one referencing earlier bindings. It is evaluated lazily — only when the value is actually missing.
function make({ width = 100, height = width }: { width?: number; height?: number } = {}) {
return width + "x" + height
}
console.log(make({ width: 50 })) // 50x50Array Defaults Recap
Defaults work in array destructuring too, applying per position when an element is missing or undefined.
const [a = 1, b = 2, c = 3] = [10, undefined]
console.log(a, b, c) // 10 2 3When Defaults Shine
Defaults are ideal for options objects: callers specify only what they care about, and the function fills sensible fallbacks. They also remove undefined from inferred types, simplifying the body.
interface Config { debug?: boolean; level?: number }
const init = ({ debug = false, level = 1 }: Config = {}) => debug ? level : 0
console.log(init({ debug: true, level: 5 }))Quick Check
Test your understanding of destructuring defaults.
Recap
Destructuring defaults (= fallback) apply only when a value is missing or undefined — never for other falsy values. They remove undefined from optional-property types, can be inferred or annotated, support renaming, and may reference earlier bindings. For options objects, default the parameter to {} so callers can omit it entirely.
Frequently asked questions
Is the “Default Values in Destructuring” lesson free?
Yes — the full text of “Default Values in Destructuring” is free to read here on the web, and the TypeScript 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 TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Default Values in Destructuring”?
Combine defaults with type annotations safely. You practise TypeScript 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 TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Default Values in Destructuring” 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 TypeScript Academy lesson?
Yes. Every TypeScript 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
- Object Destructuring Types
- Array and Tuple Destructuring
- Default Values in Destructuring
- Nested Destructuring Patterns