Typescript

Functions

  • Functions in TypeScript are the basic building blocks that allow you to encapsulate reusable code.
  • They allow you to define specific tasks and operations, ensuring type safety through parameter annotations and return types.
  • Below are the key aspects of functions in TypeScript, explained in bullet points with coding examples.
  1. Basic Function Syntax

    • Defining a Function: Use the function keyword followed by the function name, parameters with types, and an optional return type.
      function greet(name: string): string {
      return `Hello, ${name}!`;
      }

      console.log(greet(“Alice”)); // Outputs: Hello, Alice!

  2. Parameter  Types

    • Typing Parameters: You can specify the type of each parameter in the function signature.
      function multiply(a: number, b: number): number {
      return a * b;
      }

      console.log(multiply(5, 3)); // Outputs: 15

  3. Return Types

    • Specifying Return Type: The return type is defined after the parameter list using a colon.
      function getTime(): number {
      return new Date().getTime();
      }

      console.log(getTime()); // Outputs the current timestamp

    • Void Return Type: Use `void` to indicate that a function does not return a value.
      function printHello(): void {
      console.log("Hello!");
      }

      printHello(); // Outputs: Hello!
  4. Optional Parameters

    • Marking Optional Parameters: Use the ? operator to indicate that a parameter is optional.
      function add(a: number, b: number, c?: number): number {
      return a + b + (c || 0);
      }

      console.log(add(2, 3)); // Outputs: 5
      console.log(add(2, 3, 4)); // Outputs: 9
  5. Default Parameters

    • Setting Default Values: You can provide default values for parameters that will be used if no argument is passed.
      function pow(value: number, exponent: number = 2): number {
      return value ** exponent;
      }

      console.log(pow(3)); // Outputs: 9 (3^2)
      console.log(pow(3, 3)); // Outputs: 27 (3^3)
  6. Named Parameters

    • Using Object Destructuring: You can use named parameters by passing an object as an argument.
      function divide({ dividend, divisor }: { dividend: number; divisor: number }): number {
      return dividend / divisor;
      }

      console.log(divide({ dividend: 10, divisor: 2 })); // Outputs: 5
  7. Rest Parameters

    • Using Rest Parameters: You can use rest parameters to accept an indefinite number of arguments as an array.
      function add(a: number, b: number, ...rest: number[]): number {
      return a + b + rest.reduce((p, c) => p + c, 0);
      }

      console.log(add(1, 2)); // Outputs: 3
      console.log(add(1, 2, 3, 4, 5)); // Outputs: 15
  8. Function Types and Type Aliases

    • Defining Function Types: You can create type aliases for functions to specify their signature.
      type Negate = (value: number) => number;
      const negateFunction: Negate = (value) => -value;

      console.log(negateFunction(5)); // Outputs: -5

Conclusion

  • Functions in TypeScript provide powerful options for creating reusable code with strong type safety. By leveraging parameter types, return types, optional and default parameters, remaining parameters, and named parameters, you can write more robust and maintainable code.
  • Understanding these aspects will help you use the full potential of features in your TypeScript applications.