Typescript Function

Optional and default parameter

"use strict";

function hello(name: string = 'jason', age?:number) : string {
    if(age){
        console.log(age);
    }
    return name;
}

let result = hello("angela", 18);
console.log(result);

Rest parameters

"use strict";

function hello(name: string, ...userIds: number[]) : void {
    console.log(name);
    userIds.forEach(element => {
        console.log(element);
    });
}

hello("jason", 1,2,3);

overloading

"use strict";

function getBook(id : number) : void;
function getBook(title:string) : void;

function getBook(bookProperty: any) : void{    
    if(typeof bookProperty == 'string'){
        console.log(`book title: ${bookProperty}`);
    }
    else if(typeof bookProperty == 'number') {
        console.log(`book id: ${bookProperty}`);
    }    
}

getBook(1);
getBook("Harry potter");