1. Introduction
Not every function argument is required every time a function is called. TypeScript lets you mark a parameter as optional with a ?, or give it a default value that is used automatically when the caller omits it. Both features make function signatures more flexible while still keeping full type safety.
Cricket analogy: A bowler's field-setting function might take an optional sweeper?: boolean for when a captain doesn't specify one, or default overs: number = 1 so a spell defaults to a single over unless the captain calls for more — both stay fully type-checked.
Understanding the difference between optional parameters and default parameters — and the ordering rules TypeScript enforces around them — is essential for writing function signatures that behave predictably for every caller.
Cricket analogy: Knowing whether a setField(position, sweeper?) call leaves sweeper as undefined versus a setField(position, overs = 1) call substituting 1 matters for every captain calling the function, so the ordering rules keep every bowling-change call predictable.
2. Syntax
// Optional parameter (may be omitted; type becomes T | undefined)
function greet(name: string, title?: string): string {
return title ? `Hello, ${title} ${name}` : `Hello, ${name}`;
}
// Default parameter (value used when argument is omitted or undefined)
function createUser(name: string, role: string = "member"): string {
return `${name} (${role})`;
}
greet("Ada");
greet("Ada", "Dr.");
createUser("Sam");
createUser("Sam", "admin");3. Explanation
An optional parameter is declared with a ? after its name, e.g. title?: string. Inside the function body its type is string | undefined, so you must narrow it (with an if or ??) before using it as a plain string. A default parameter, declared as role: string = "member", is automatically optional for callers — TypeScript infers its type from the default value if no annotation is given, and substitutes the default whenever the argument is omitted or explicitly passed as undefined.
Cricket analogy: A nickname?: string field gives string | undefined, so a commentator must narrow it with ?? before reading it aloud, while role: string = "member" auto-fills a new squad player's role to "member" whenever it's omitted or passed as undefined.
TypeScript enforces an ordering rule: every required parameter must come before any optional (?) or default parameter in the parameter list, unless the optional/default parameter is followed only by other optional parameters. This mirrors JavaScript's positional argument passing — there is no way to skip a middle argument except by passing undefined explicitly.
Cricket analogy: TypeScript's rule that bowlOver(bowler, overNumber?, sweeper?) can't put a required param after an optional one mirrors how a scorer can't skip announcing the bowler's name mid-over without explicitly saying "unknown" — there's no positional gap allowed except passing undefined on purpose.
Passing undefined explicitly for a default parameter still triggers the default value, e.g. createUser("Sam", undefined) returns "Sam (member)", because TypeScript's default parameters use the same substitution rule as plain JavaScript default parameters.
A required parameter can never follow an optional or default parameter positionally, e.g. function f(a?: string, b: string) is a compile error. TypeScript requires all required parameters to be declared first, so reorder the parameter list rather than trying to make an earlier parameter optional just for convenience.
4. Example
function buildInvoice(customer: string, amount: number, taxRate: number = 0.1, note?: string): void {
const tax = amount * taxRate;
const total = amount + tax;
let line = `${customer}: $${total.toFixed(2)}`;
if (note !== undefined) {
line += ` (${note})`;
}
console.log(line);
}
buildInvoice("Nina", 100);
buildInvoice("Omar", 200, 0.2);
buildInvoice("Priya", 50, undefined, "rush order");5. Output
Nina: $110.00
Omar: $240.00
Priya: $55.00 (rush order)6. Key Takeaways
- A
?after a parameter name makes it optional; its type inside the function becomesT | undefined. - A default value (
param: Type = value) makes a parameter optional and supplies a fallback automatically. - TypeScript infers the parameter's type from the default value if no explicit annotation is given.
- Passing
undefinedexplicitly for a default parameter still triggers the default value. - Required parameters must be declared before optional or default parameters in the parameter list.
- Optional parameters need explicit
undefined/truthiness checks before use; default parameters do not.
Practice what you learned
1. What is the type of `title` inside the function body given `function greet(name: string, title?: string) {}`?
2. Given `function createUser(name: string, role = "member") {}`, what type does TypeScript infer for `role`?
3. What does `createUser("Sam", undefined)` return given `function createUser(name: string, role: string = "member") { return role; }`?
4. Which of the following function signatures fails to compile?
5. Why must you check for `undefined` before using an optional parameter's value, but not a default parameter's value?
Was this page helpful?
You May Also Like
Functions in TypeScript
Learn how TypeScript adds parameter and return type annotations to JavaScript functions for compile-time safety.
Rest Parameters in TypeScript
Collect an arbitrary number of trailing arguments into a typed array parameter.
Function Overloads in TypeScript
Declare multiple callable signatures for a single function so callers get precise types for each usage pattern.
Type Inference in TypeScript
How TypeScript figures out types automatically from values, without explicit annotations.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics