100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript

Optional and Default Parameters in TypeScript

Make function parameters optional or give them default values, and understand how TypeScript infers their types.

FunctionsBeginner8 min readJul 8, 2026
Analogies

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

typescript
// 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

typescript
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

text
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 becomes T | 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 undefined explicitly 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

Was this page helpful?

Topics covered

#TypeScript#TypeScriptProgrammingStudyNotes#Programming#OptionalAndDefaultParametersInTypeScript#Optional#Default#Parameters#Syntax#StudyNotes#SkillVeris