Default Parameters in Arrow Functions
You'll learn how to combine arrow function syntax with default parameters while preserving TypeScript's type safety.
The Core Idea
Arrow functions can use default parameters exactly like regular functions — the syntax works identically. TypeScript infers the parameter type from the default value, but you can also add explicit type annotations for clarity or constraints.
Think of default parameters in arrow functions like pre-filled form fields: whether the form is paper (regular function) or digital (arrow function), the default values work the same way.
Basic Syntax
const greet = (name: string = "Guest") => {
return `Hello, ${name}!`;
};
greet(); // "Hello, Guest!"
greet("Alice"); // "Hello, Alice!"
The parameter name gets the inferred type string from its default value.
Type Inference with Defaults
const multiply = (a: number, b = 2) => a * b;
// b is inferred as number
const result = multiply(5); // 10
const explicit = multiply(5, 3); // 15
Combining with Optional Parameters
Remember: default parameters are implicitly optional, so you don't need the ? operator:
// Correct
const format = (value: string, uppercase = false) => {
return uppercase ? value.toUpperCase() : value;
};
// Redundant — don't do this
const redundant = (value: string, uppercase?: boolean = false) => {
return uppercase ? value.toUpperCase() : value;
};
Multiple Defaults in Arrow Functions
const createUser = (name = "Anonymous", age = 0, active = true) => ({
name,
age,
active
});
const user = createUser("Bob", 25);
// { name: "Bob", age: 25, active: true }
Key Takeaway: Arrow functions support default parameters with the same syntax and type inference rules as regular functions — the arrow syntax doesn't change how defaults work, only how the function is declared.