TypeScript allows a wide variety of parameter types in its functions. Let’s see each of them.
If you want to learn more about Function Parameters
check 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 function
greet
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, 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 function greet
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 will be displayed that includes the age. If no value is provided for age
, a generic message will be displayed.
Default Parameters
TypeScript also allows you to specify default values for parameters of a function (this means that if a value is not 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
The 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 parameter mode
can only accept the values "light"
or "dark"
, ensuring that only these two specific values can be used.
Function Parameters as Types
TypeScript allows the use of function types as parameters, which allows 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.