Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 688 Bytes

File metadata and controls

24 lines (21 loc) · 688 Bytes

Section 6.3: Functions with Union Types

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;
  }
}