Jarod Cavalcante
2020-12-24T19:40:58Z

union&typeallias.ts

| /** Type Allias e Union */
|
| function logDetails(uid: number | string, item: string) {
| console.log(`A product with ${uid} has a title as ${item}`)
| }
|
| // uid: number | string => union
|
| logDetails(123, 'sapato')
| logDetails('123', 'sapato')
|
| function logInfo(uid: number | string, user: string) {
| console.log(`A user with ${uid} has a name as ${user}`)
| }
|
| logInfo('123', 'jarod')
| logInfo(123, 'jarod')
|
| // type allias
| type Uid = number | string | undefined
|
| function logDetails2(uid: Uid, item: string) {
| console.log(`A product with ${uid} has a title as ${item}`)
| }
|
| function logInfo2(uid: Uid, user: string) {
| console.log(`A user with ${uid} has a name as ${user}`)
| }
|
| logInfo2('123', 'jarod')
| logInfo2(123, 'jarod')
|
| type Platform = 'Windows' | 'Linux' | 'MacOS'
|
| function handlePlatform(platform: Platform) {
| return platform
| }
|
| handlePlatform('Windows') // work
| handlePlatform('ios') // dont work