# TypeScript 常用型別 - 聯合型別(Union Types)  多型別皆可 ``` 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') // 會出錯,因有非對應型別 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up