TypeScript allows a great variety of parameter types in its functions. Let’s look at each of them
If you want to learn more, check out the Introduction to Programming Course
Required Parameters
Required parameters are those that must be provided when the function is called. 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,
nombreandedadare required parameters.- The
saludarfunction 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 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 saludar function receives a required parameter nombre of type string and an optional parameter edad of type number.
If a value for edad is provided, a personalized message including the age will be displayed. If no value for edad is provided, a generic message will be displayed.
Default Parameters
TypeScript also allows specifying default values for function parameters (this means that if a value for a parameter is not provided, 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 edad parameter has a default value of 18. If no value for edad is provided 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, numeros is a rest parameter that allows the sumar 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 modo parameter can only accept the values "light" or "dark", ensuring that only these two specific values can be used.
Function Parameters as a 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, ejecutarOperacion accepts two numbers and a function operacion that defines how those numbers should be combined.
