undefined vs Omission
What you'll learn: How TypeScript treats explicitly passing undefined differently from simply leaving out an optional parameter.
The Subtle Difference
When a parameter is optional, you have two ways to "skip" it: you can omit it entirely at the call site, or you can explicitly pass undefined. While these might seem identical, TypeScript treats them the same way — both are valid and acceptable.
Think of it like declining food at a party: you can simply not take any (omission), or you can say "no thanks" and actively refuse it (passing undefined). The host doesn't mind either way, and you end up with the same result — no food on your plate.
How It Works in Practice
function greet(name?: string) {
console.log(name); // undefined either way
}
// Omitting the parameter
greet(); // totally fine
// Explicitly passing undefined
greet(undefined); // also totally fine
// Both result in name being undefined inside the function
Both calls are valid. Inside greet, the name parameter will be undefined in both cases. TypeScript allows both patterns because they're semantically equivalent — the parameter wasn't provided.
Why This Matters
This becomes relevant when you're working with callbacks or passing parameters conditionally:
function process(value: number, callback?: (x: number) => void) {
if (callback) {
callback(value);
}
}
const shouldLog = false;
const logger = shouldLog ? (x: number) => console.log(x) : undefined;
process(42, logger); // explicitly passing undefined is fine
Key Takeaway: In TypeScript, omitting an optional parameter and explicitly passing undefined are both valid and produce the same result — the parameter receives undefined inside the function.