Author : MD TAREQ HASSAN

Optional parameter


function totalLength(x: string, y?: string)): number { // param y is optional

	// ... ... ...
	
	
	return 1;
}

Overloading

function totalLength(x: string, y: string): number
function totalLength(x: any[], y: any[]): number
function totalLength(x: (string | any[]), y: (string | any[])): number {

    let total: number = x.length + y.length;
    
    x.slice(0)
    
    if(x instanceof Array) {
        x.push('TypeScript')
    }
    
    if(x instanceof String) {
        x.substr(0)
    }
    
    return total;
}

totalLength();

Union type parameter

function totalLength(x: (string | any[]), y: (string | any[])): number {

    let total: number = x.length + y.length;
    
    x.slice(0)
    
    if(x instanceof Array) {
        x.push('TypeScript')
    }
    
    if(x instanceof String) {
        x.substr(0)
    }
    
    return total;
}

See: https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types