Default Parameters Are Implicitly Optional
What you'll learn: You'll discover that function parameters with default values don't need the ? operator because TypeScript already treats them as optional.
The Core Idea
When you give a parameter a default value, TypeScript automatically understands that callers can skip that parameter. You don't need to add ? to mark it optional — the default value already communicates that.
Think of it like a restaurant menu with combo meals: if a meal comes with fries by default, you don't need to order fries separately. The default is already included, and it's automatically optional to change.
How It Works
When you write a function with a default parameter:
function greet(name: string, greeting: string = "Hello") {
return `${greeting}, ${name}!`;
}
Notice greeting has no ?, yet both of these calls work perfectly:
greet("Alice"); // "Hello, Alice!"
greet("Bob", "Good morning"); // "Good morning, Bob!"
The greeting parameter is implicitly optional because it has a default value. If the caller omits it, TypeScript knows what to use.
Why This Makes Sense
Adding ? would be redundant:
// Unnecessary! The default already makes it optional
function greet(name: string, greeting?: string = "Hello") {
return `${greeting}, ${name}!`;
}
TypeScript infers the type from your default value and knows the parameter can be skipped. The default value serves both purposes: providing a fallback and signaling optionality.
Key Takeaway: Parameters with default values don't need ? — they're automatically optional because the default tells TypeScript what to use when the argument is omitted.