- TypeScript Tutorial
- TypeScript Home
- TypeScript Introduction
- TypeScript Setup
- TypeScript First Program
- TypeScript vs JavaScript
- TypeScript Data Types
- TypeScript Type Inference
- TypeScript Type Annotations
- TypeScript Interfaces
- TypeScript Enums
- TypeScript Type Aliases
- TypeScript Type Assertions
- TypeScript Variables
- TypeScript Functions
- TypeScript Functions
- TypeScript Optional Parameters
- TypeScript Default Parameters
- TypeScript Rest Parameters
- TypeScript Arrow Functions
- Classes and Objects
- Introduction to Classes
- Properties and Methods
- Access Modifiers
- Static Members
- Inheritance
- Abstract Classes
- Interfaces vs Classes
- Advanced Types
- TypeScript Union Types
- TypeScript Intersection Types
- TypeScript Literal Types
- TypeScript Nullable Types
- TypeScript Type Guards
- TypeScript Discriminated Unions
- TypeScript Index Signatures
- TypeScript Generics
- Introduction to Generics
- TypeScript Generic Functions
- TypeScript Generic Classes
- TypeScript Generic Constraints
- TypeScript Modules
- Introduction to Modules
- TypeScript Import and Export
- TypeScript Default Exports
- TypeScript Namespace
- Decorators
- Introduction to Decorators
- TypeScript Class Decorators
- TypeScript Method Decorators
- TypeScript Property Decorators
- TypeScript Parameter Decorators
- Configuration
- TypeScript tsconfig.json File
- TypeScript Compiler Options
- TypeScript Strict Mode
- TypeScript Watch Mode
TypeScript Optional Parameters
In TypeScript, optional parameters allow you to define parameters that may or may not be provided when calling a function. These parameters are indicated by placing a ?
symbol after the parameter name in the function signature.
Syntax for Optional Parameters
In this syntax, param2
is optional, meaning the function can be called with or without it.
Example of Optional Parameters
Here’s a basic example to illustrate how optional parameters work:
In this example:
- The
age
parameter is optional. - If
age
is not provided, it will not be included in the greeting message.
Default Values for Optional Parameters
Optional parameters can also have default values. If no argument is provided for the parameter, the default value will be used.
In this case, if the age
parameter is not passed, it defaults to 30
.
Optional Parameters with Rest Parameters
You can combine optional parameters with rest parameters. Rest parameters allow you to pass an arbitrary number of arguments to a function, while optional parameters can still be used for specific values.
Here:
- The
age
parameter is optional. - The
hobbies
parameter is a rest parameter, allowing any number of hobbies to be passed.
Function Overloading with Optional Parameters
You can use function overloading in combination with optional parameters to create multiple signatures for a function. This allows different numbers of parameters to be passed without causing type errors.
Here:
- The first signature accepts only the
name
parameter. - The second signature accepts both
name
andage
. - The implementation provides logic for both cases.
Summary
Optional parameters in TypeScript offer flexibility in function definitions by allowing parameters to be omitted when calling a function. You can use them with default values, rest parameters, and function overloading to create more versatile and robust functions. By using optional parameters, you can ensure that your functions remain usable even when not all data is available.