Functions in TypeScript

As we know no data type check is made while creating functions in JavaScript but in type script we can do that by passing Types as parameters in function declaration.

Lets see first the traditional JavaScript function call example :

Ouput looks like this :

Now lets write the same function in TypeScript

function add(a : number , b : number){
    return a+b;
}
console.log("Adding two numbers : "+add(10,30));
console.log("Adding String and number : "+add("Hello",30));

The above code does not compile since we are trying to pass String instead of number

Hence after removing the line where String is given as parameter into the add function the code compiles are gives successful result

function add(a : number , b : number){
    return a+b;
}
console.log("Adding two numbers : "+add(10,30));

Optional and Default argument in Function

  • We can specify optional argument in TypeScript by adding ? after the variable name.
  • This optional argument always has to be at the end of the method parameter.
  • Multiple optional arguments are allowed.
  • By specifying the default value in the function parameter makes the parameter optional.
  • We can specify the default value by assigning value to it
    example function  add(a : number , b : number, c : number = 10) now c has default value has 10 and if 3rd parameter is not passed still it will be considered as 10.
/**
 * 
 * @param a 
 * @param b 
 * @param c - Optional parameter by adding ? in the function argument definition
 * Note : Optional argument can always be at the end of the function argument
 */
function add(a : number , b : number, c : number = 50,d? : number){
    if(d==null){
        d=0;
    }
    return a+b+c+d;
}
console.log("Adding two numbers, default and optional : "+add(10,20));
console.log("Adding three numbers and optional : "+add(10,20,30));
console.log("Adding four numbers : "+add(10,20,30,40));

Specifying function return type

  • The way to specify the return type of function is by adding a : type after closing bracket of the function parameters.
  • Syntax
    function functionName (a: type, b : type) : return_type{ }
  • If if add return type as number and pass return type as String then it will throw error while compilation
function add(a : number, b : number) : number{
    return "Hello";
}
function add(a : number, b : number) : number{
    return a+b;
}

Declaring a a variable as Function type :

let abc : Function;
//abc = 'Hello'; does not compile since string is assigned
abc = function(){
    console.log('Hello Function');
}

Function Signature

// example 1
let greet: (a: string, b: string) => void;

greet = (name: string, greeting: string) => {
  console.log(`${name} says ${greeting}`);
}

// example 2
let calc: (a: number, b: number, c: string) => number;

calc = (numOne: number, numTwo: number, action: string) => {
  if (action === 'add') {
    return numOne + numTwo;
  } else {
    return numOne - numTwo;
  }
}

// example 3
let logDetails: (obj: {name: string, age: number}) => void;

logDetails = (ninja: {name: string, age: number}) => {
  console.log(`${ninja.name} is ${ninja.age} years old`);
}

Leave a Comment