Optional and Default Parameters
Handle missing arguments with ? and default values.
Welcome
TypeScript lets you mark function parameters as optional with `?` or give them default values. This makes APIs flexible while staying type-safe.
Optional Parameters with ?
Add `?` after a parameter name to make it optional. The parameter type becomes T | undefined inside the function.
function greet(name: string, greeting?: string): string {
return (greeting ?? 'Hello') + ', ' + name;
}All lessons in this course
- Parameter and Return Type Annotations
- Optional and Default Parameters
- Rest Parameters and Spread with Types
- void, never, and Function Signatures