# TypeScript 常用型別 - 聯合型別(Union Types) ![image](https://hackmd.io/_uploads/B1tHDw7zC.png) 多型別皆可 ``` let id: string | number; id = '12345' id = 3333 id = true // 會出錯,因為非對應型別 ``` ``` let names : sting | string[]; names = 'alice'; names = ['alice','Bob',123] // 因有非對應型別 ``` ``` function displayPrice(price: number|[number,number]) { if(typrof price === 'number'){ console.log(`價格範圍:${price}`) }else{ console.log(`價格範圍:${price[0]}~${price[1]}`) > } } displayPrice(40) displayPrice([33,55]) d0isplayPrice('40') // 會出錯,因有非對應型別 ```