TypeScript Function, Return types, Rest Parameters

Functions are an essential part of any programming language, including TypeScript. They allow you to group together a set of statements that perform a specific task, and can be called multiple times throughout your code.

In this article, we will explore the different aspects of functions in TypeScript, including function declaration and parameters, return types, optional and default parameters, and rest parameters.

Note: The function declaration, parameters, return types, optional, default parameters are almost same as JavaScript, just added types with it in TypeScript.

Function Declaration and Parameters

In TypeScript, you can declare a function using the function keyword same as JavaScript, followed by the name of the function and a set of parentheses that contain any parameters for the function.
Here’s an example:

function add(a: number, b: number) {
  return a + b;
}

In this example, we declare a function called add() that takes two parameters, both of type number. Inside the function, we add the two parameters together and return the result a + b.

You can also declare the type of the function parameters using type annotations, as shown in the example above. Variable a type is number and variable b type is also number.

a: number, b: number

Return Types

In TypeScript, you can also specify the return type of a function using a type annotation. Here’s an example:

function multiply(a: number, b: number): number {
  return a * b;
}

In this example, we define a function called multiply() that takes two parameters of type number, and returns a value of type number. Inside the function, we multiply the two parameters together and return the result a * b.

Optional and Default Parameters

In TypeScript, you can make a function parameter optional by adding a question mark ? after its name. Here’s an example:

function greet(name?: string) {
  if (name) {
    console.log(`Hello, ${name}!`);
  } else {
    console.log(`Hello, stranger!`);
  }
}

In this example, we define a function called greet() that takes an optional parameter called name of type string. If the parameter is provided, we log a greeting message that includes the name. If the parameter is not provided, we log a generic greeting message.

You can also provide default values for function parameters by assigning a value to them in the parameter list. Here’s an example:

function calculateDiscount(price: number, discount: number = 0.1) {
  return price - (price * discount);
}

In this example, we define a function called calculateDiscount() that takes two parameters, price of type number, and discount of type number, which has a default value of 0.1. Inside the function, we calculate the discount and subtract it from the price to get the final discounted price.

Rest Parameters

In TypeScript, you can also use rest parameters to allow a function to accept any number of arguments of the same type. Rest parameters are denoted by an ellipsis … followed by the parameter name. Here’s an example:

function sum(...numbers: number[]) {
  let total = 0;
  for (let number of numbers) {
    total += number;
  }
  return total;
}

In this example, we define a function called sum that accepts any number of arguments of type number and it’s array of numbers like this way - number[].
Inside the function, we loop through the arguments and add them together to get the total sum.

Conclusion

Functions are an essential part of any programming language, and TypeScript provides a wide range of features to make working with functions more convenient and efficient.

By understanding the different aspects of functions in TypeScript, including function declaration and parameters, return types, optional and default parameters, and rest parameters, you can write cleaner, more efficient, and more robust code.

Previous
TypeScript Variable, Annotation and Inference Learning
Next
Interfaces in TypeScript - Learn TypeScript Interface in Depth