Skip to content

Instantly share code, notes, and snippets.

@jarodsim
Created December 24, 2020 19:40
Show Gist options
  • Save jarodsim/64affec360cb7175e50b7d2c866d6df8 to your computer and use it in GitHub Desktop.
Save jarodsim/64affec360cb7175e50b7d2c866d6df8 to your computer and use it in GitHub Desktop.
/** 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment