Functions are an essential part of programming, and TypeScript provides a robust and powerful way to define them. In this tutorial, we'll take a closer look at functions in TypeScript and explore how to create and use them effectively in your code.

Defining Functions

In TypeScript, you can define a function using the function keyword, followed by the function name, and then the parameter list in parentheses. Here's an example:

function addNumbers(num1: number, num2: number): number {
  return num1 + num2;
}

In this example, we've defined a function called addNumbers that takes two parameters, both of which are of type number, and returns a value of type number. The : number syntax at the end of the parameter list and before the opening brace specifies the return type of the function.


Optional and Default Parameters

In TypeScript, you can specify optional parameters by appending a ? to the parameter name. Optional parameters are used when you want to make a parameter optional, meaning that you can call the function with or without that parameter. Here's an example:

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

In this example, we've defined a function called greet that takes an optional name parameter of type string. If the name parameter is provided, the function will print a personalized greeting. If the name parameter is not provided, the function will print a generic greeting.

You can also specify default parameter values by assigning a value to the parameter. Here's an example:

function calculateInterest(principal: number, rate: number, time: number = 1) {
  return (principal * rate * time) / 100;
}

In this example, we've defined a function called calculateInterest that takes three parameters: principal, rate, and time. The time parameter has a default value of 1, which means that if the time parameter is not provided, it will default to 1.


Rest Parameters

In TypeScript, you can use rest parameters to pass an indefinite number of arguments to a function. Rest parameters are denoted by the ... syntax before 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've defined a function called sum that takes an indefinite number of arguments of type number. The function uses a for loop to iterate over the numbers and adds them together to return the total.


Arrow Functions

In TypeScript, you can also define functions using arrow function notation. Arrow functions are a shorthand way of writing functions that use the => syntax. Here's an example:

const square = (num: number) => num * num;

In this example, we've defined an arrow function called square that takes a parameter num of type number and returns the square of that number.


Conclusion

In this tutorial, we've explored how to define functions in TypeScript, including optional and default parameters, rest parameters, and arrow functions. By understanding these concepts, you can create more powerful and flexible functions in your TypeScript code.