TypeScript allows a wide variety of parameter types in its functions. Let’s look at each one of them.
If you want to learn more about Function Parameters
check out the Introduction to Programming Course read more
Required Parameters
Required parameters are those that must be provided when calling the function. These parameters are defined in the function declaration without any special modifier.
function greet(name: string, age: number): void {
console.log(`Hello ${name}, you are ${age} years old.`);
}
greet("Ana", 30); // Output: Hello Ana, you are 30 years old.
In this example,
name
andage
are required parameters.- The
greet
function requires both parameters to be provided at the time of the call.
Optional Parameters
In TypeScript, we can also make function parameters optional (this means that it is not necessary to provide a value for those parameters when calling the function).
To make a parameter optional, we simply add a question mark (?
) after the parameter name.
function greet(name: string, age?: number): void {
if (age) {
console.log(`Hello ${name}, you are ${age} years old.`);
} else {
console.log(`Hello ${name}.`);
}
}
In this example, the greet
function receives a required parameter name
of type string
and an optional parameter age
of type number
.
If a value is provided for age
, a personalized message including the age will be displayed. If no value is provided for age
, a generic message will be shown.
Default Parameters
TypeScript also allows specifying default values for function parameters (this means that if no value is provided for a parameter, the specified default value will be used).
To assign a default value to a parameter, we use the assignment operator (=
) after the data type.
function greet(name: string, age: number = 18): void {
console.log(`Hello ${name}, you are ${age} years old.`);
}
In this case, the parameter age
has a default value of 18
. If no value is provided for age
when calling the function, the default value will be used.
Rest Parameters
Rest
parameters (or variadic parameters) allow a function to accept a variable number of arguments. They are defined using three dots (...
) before the parameter name and are collected into an array.
function sum(...numbers: number[]): number {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10
console.log(sum(5, 10)); // Output: 15
In this example, numbers
is a rest
parameter that allows the sum
function to accept any number of numeric arguments and return their sum.
Parameters with Literal Types
In TypeScript, you can define parameters that only accept specific values using literal types. This is useful when you want to restrict the allowed values for a parameter.
function setMode(mode: "light" | "dark"): void {
console.log(`Selected mode: ${mode}`);
}
setMode("light"); // Output: Selected mode: light
setMode("dark"); // Output: Selected mode: dark
In this case, the mode
parameter can only accept the values "light"
or "dark"
, ensuring that only these two specific values can be used.
Function Parameters as Type
TypeScript allows using function types as parameters, which enables passing functions as arguments to other functions.
function executeOperation(a: number, b: number, operation: (x: number, y: number) => number): number {
return operation(a, b);
}
const add = (x: number, y: number): number => x + y;
const subtract = (x: number, y: number): number => x - y;
console.log(executeOperation(5, 3, add)); // Output: 8
console.log(executeOperation(5, 3, subtract)); // Output: 2
In this example, executeOperation
accepts two numbers and an operation
function that defines how those numbers should be combined.