Optional, Default & Rest Parameters
Beginner · 15 min read · ▶ live playground · ✦ checkpoint
Optional, default, and rest parameters let a function accept flexible inputs without dropping type safety. Use ? when an argument may be missing, = value when the function should fill in a fallback, and ...items: string[] when callers may pass a list.
Optional means possibly undefined
An argument is the value you pass into a function. An omitted argument is one you leave out when you call the function. An optional parameter is a parameter marked with ?, meaning callers may omit that argument.
nickname?: string does not mean "maybe present, but treat it like a string." It means the allowed value set is string | undefined. A union type is an either-or type written with |; lesson 4.1 teaches that model fully. For now, read string | undefined as "text, or the missing value."
That is why the function checks nickname === undefined before building the label. If you skip that check and use a string method immediately, TypeScript 5.9 stops you:
function shoutNickname(nickname?: string): string {
return nickname.toUpperCase();
}→ playground.ts:2:10 - error TS18048: 'nickname' is possibly 'undefined'.
The ? adds undefined to the promise. The checker is doing set logic: .toUpperCase() is safe for string, but not for undefined.
Defaults fill the missing value
A default parameter is a parameter with a value after =, like seats = 1. A default value is the fallback JavaScript uses when the caller omits that argument or passes undefined.
Notice the difference from ?. Inside describePlan, seats is usable as a number. The default runs before the body, so by the time your code reaches seats.toFixed(0), seats has a number value.
The value after = also gives TypeScript a value to infer from. seats = 1 infers seats as number, and callers may still leave the argument out. If the default is not obvious to readers, writing seats: number = 1 is also fine; the promise is the same.
Keep the two tools separate:
nickname?: stringmeans you handlestring | undefinedinside the function.seats = 1means the function fills in anumberbefore the body runs.
Rest gathers the remaining arguments
A rest parameter is a parameter with ... that gathers zero or more trailing arguments into one array. Because the variable inside the function is an array, you annotate it with an array type.
...milestones: string[] means every extra argument after owner must be a string, and inside the function milestones is a string[]. That is the same array label from Section 2, now used at a function boundary.
Rest parameters are good for commands like "tag this launch with these labels" or "record these messages." They are not the same as an optional parameter. Optional means one slot may be missing. Rest means the caller may give zero, one, or many values for the trailing list.
Put optional slots after required slots
An optional-parameter ordering rule is TypeScript's rule that a required parameter cannot come after an optional ? parameter. The reason is call clarity. If the first slot may be skipped but the second slot is required, a call with one argument becomes hard to read and easy to get wrong.
function scheduleLaunch(region?: string, day: string): string {
const place = region === undefined ? "remote" : region;
return day + " in " + place;
}→ playground.ts:1:42 - error TS1016: A required parameter cannot follow an optional parameter.
Write the required promise first, then the flexible one:
function scheduleLaunch(day: string, region?: string): string {
const place = region === undefined ? "remote" : region;
return day + " in " + place;
}
console.log(scheduleLaunch("Friday"));
console.log(scheduleLaunch("Monday", "Mumbai"));→ Friday in remote
→ Monday in Mumbai
That order lets the call read from left to right: the function needs a day, and may also receive a region.
The rule to keep
Use ? when the body must handle undefined. Use a default when the function has a real fallback value. Use a rest parameter when the caller can pass a trailing list, and put flexible slots after required ones.
Next, functions themselves become values. Once parameters are typed, you can pass whole functions around as callbacks and give those callback shapes their own type promises.
Checkpoint
Answer all three to mark this lesson complete