Functions with Union Types

suggest change

A TypeScript function can take in parameters of multiple, predefined types using union types.

function whatTime(hour:number|string, minute:number|string):string{
    return hour+':'+minute;
}

whatTime(1,30)         //'1:30'
whatTime('1',30)       //'1:30'
whatTime(1,'30')       //'1:30'
whatTime('1','30')     //'1:30'

Typescript treats these parameters as a single type that is a union of the other types, so your function must be able to handle parameters of any type that is in the union.

function addTen(start:number|string):number{
    if(typeof number === 'string'){
        return parseInt(number)+10;
    }else{
        else return number+10;
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents