1. Union (ν©μ§ν©, |)
Union(μ λμΈ) : νλλ‘ λμ³μ§λ κ², μ‘°ν©
- e.g. Unit : νλλ‘ μμ§μ΄λ μ΅μ λ¨μ, (μνμ) 1κ°, (νΉμ μ무λ₯Ό μν) λΆλ | λ¨μ²΄
- e.g. Unity : νλλ‘ λμ³μ§ μ¬λλ€μ λ§μ, μ μ λ±
1// 4κ°μ§ νμ μ κ°μ§ μ μλ Direction νμ ----------------------2type Direction = 'left' | 'right' | 'up' | 'down'3function move(direction: Direction) {4console.log(direction)5}6move('down')78// 3κ°μ§ νμ μ κ°μ§λ TileSizeνμ -------------------------------9type TileSize = 8 | 16 | 3210const tile: TileSize = 16
2. Intersection (κ΅μ§ν©, &)
Intersect(μΈν°μΉμ
) : κ΅μ°¨νλ€[λ§λλ€], (μ΄λ€ μ§μμ) κ°λ‘μ§λ₯΄λ€, κ΅μ§ν©
1type student = {2name: string3score: number4}56type father = {7empolyeeId: number8work: () => void9}1011// νμ & μ§μ₯μΈ νμ μ λͺ¨λ μμ±ν΄μΌλ§ νλ κ²½μ°12function internWork(μ§μ: student & father) {13console.log(μ§μ.name, μ§μ.empolyeeId, μ§μ.work())14}1516internWork({17name: 'λ©μ',18score: 189,19empolyeeId: 123,20work: () => {},21})
3. Union Typeμ μΈ λ μ£Όμν μ
λ Όλ¦¬μ μΌλ‘ Unio νμ μ OR, Intersection νμ μ ANDλΌκ³ μκ°νμλ λΆλ€μ΄ μμν λ°, Interaceμ κ°μ νμ μ λ€λ£° λλ λ€μμ κ°μ λ Όλ¦¬μ μ¬κ³ λ₯Ό μ£Όμν΄μΌ ν©λλ€. μλ μ½λλ₯Ό λ³΄κ² μ΅λλ€.
1interface Person {2name: string3age: number4}5interface Developer {6name: string7skill: string8}910// νλΌλ―Έν° νμ μ `Person`, `Developer` μΈν°νμ΄μ€μ Union νμ μΌλ‘ μ μ11// νλΌλ―Έν°μ νμ μ΄ `Person`λ λκ³ `Developer`λ λλ€κ³ μκ°ν΄μ12// ν¨μ μμ ν΄λΉ μΈν°νμ΄μ€λ€μ΄ μ 곡νλ `age`λ `skill`λ₯Ό μ¬μ©ν μ μλ€λΌκ³ μκ°ν μ μμ13// κ·Έλ¬λ introduce() ν¨μλ₯Ό νΈμΆνλ μμ μ `Person` νμ μ΄ μ¬μ§ `Developer` νμ μ΄ μ¬μ§ μ μκ° μμ14// κ·Έλμ TSλ μ΄λ νμ μ΄ λ€μ΄μ€λ κ°μ μ€λ₯κ° μ λλ λ°©ν₯μΌλ‘ νμ μ μΆλ‘ ν¨15function introduce(someone: Person | Developer) {16someone.name // O μ μ λμ17someone.age // X νμ μ€λ₯18someone.skill // X νμ μ€λ₯19}
κ²°κ³Όμ μΌλ‘ introduce()μμμλ λ³λμ **νμ
κ°λ(Type Guard)**λ₯Ό μ΄μ©νμ¬ νμ
μ λ²μλ₯Ό μ’νμ§ μλ μ΄μ,
κΈ°λ³Έμ μΌλ‘λ Personκ³Ό Developer λ νμ
μ 곡ν΅μ μΌλ‘ λ€μ΄μλ μμ±μΈ nameλ§ μ κ·Όν μ μκ² λ©λλ€.
4. Type Narrowing(νμ μ’νκΈ°)
ifλ¬Έ λ±μΌλ‘ νμ μ νλλ‘ μ ν΄μ£Όλ κ²μ μλ―Έν©λλ€. νμ μ΄ νμ€νμ§ μμ λ μκΈ°λ λΆμμ©μ λ§κΈ°μν μ₯μΉμ λλ€.
NarrowingμΌλ‘ νμ ν΄μ£Όλ λ¬Έλ²λ€
- 쑰건문μ μ΄μ©ν νμ κ°λ
- typeof νμ
κ°λ :
typeof λ³μ - in νμ
κ°λ :
μμ±λͺ in μ€λΈμ νΈ μλ£ - instanceof νμ
κ°λ :
μΈμ€ν΄μ€ instanceof λΆλͺ¨
4.1 쑰건문μ μ΄μ©ν νμ κ°λ
1type Animal = {2name: string3legs?: number4}5function addLeg(animal: Animal) {6// leg μμ±μ΄ undefinedκ° λ μ μκΈ° λλ¬Έμ νμ μλ¬κ° λ°μν μ μμ7// leg μμ±μ μ¬μ©νκΈ° μ μ truthyμΈμ§ νμΈνλ©΄ λ¨8// λ³μμμ nullκ³Ό undefinedλ₯Ό μ μΈμν΄9if (animal.legs) {10animal.legs = animal.legs + 111}12}
4.2 typeof νμ κ°λ
1function PlusOne(x: number | string) {2if (typeof x === 'number') return x + 13else if (typeof x === 'string') return x + 14else return 05}
4.3 in νμ κ°λ
1interface Person {2firstName: string3surname: string4}5interface Organization {6name: string7}89type Contact = Person | Organization1011function sayHello(contact: Contact) {12// μμ±λͺ in μ€λΈμ νΈ μλ£13if ('firstName' in contact) {14console.log('Hello ' + contact.firstName)15}16}
4.4 instanceof νμ κ°λ
1class Person {2constructor(3public firstName: string,4public surname: string,5) {}6}7class Organisation {8constructor(public name: string) {}9}10type Contact = Person | Organisation1112function sayHello(contact: Contact) {13console.log('Hello ' + contact.firstName)14// π₯ Property 'firstName' does not exist on type 'Contact'.15// π₯ contactκ° firstName μμ±λ₯Ό μλ Organisation νμ μΌ μ μκΈ° λλ¬Έ16}
μΈμ€ν΄μ€ instanceof λΆλͺ¨λ₯Ό μ¬μ©νλ©΄, νΈν©λλ€.
1class Person {2constructor(3public firstName: string,4public surname: string,5) {}6}7class Organisation {8constructor(public name: string) {}9}10type Contact = Person | Organisation1112function sayHello(contact: Contact) {13// μΈμ€ν΄μ€ instanceof λΆλͺ¨14// if λ¬Έμμμ contactμ νμ μ PersonμΌλ‘ μ’νμ§(narrow)15if (contact instanceof Person) {16console.log('Hello ' + contact.firstName)17}18}