πŸ“’[TypeScript λ§ˆμŠ€ν„° with Webpack & React] Step13.νƒ€μž… 쒁히기

κΆŒμš©μ€€Β·2023λ…„ 12μ›” 6일
0
post-thumbnail

1. Typeof κ°€λ“œ


νƒ€μž… μ’νžˆκΈ°λž€ 주둜 λͺ…ν™•ν•˜μ§€ μ•Šμ€ νƒ€μž…μ΄ μžˆμ„λ•Œ μ‚¬μš©ν•œλ‹€. 그런 νƒ€μž…μ„ μœ λ‹ˆμ˜¨ νƒ€μž…μ΄λΌκ³  ν•˜λ©° 이λ₯Ό 보닀 λͺ…ν™•ν•˜κ²Œ μ’νžˆλŠ” 것이닀.

typeofλŠ” string,number,booleanκ³Ό 같은 μ›μ‹œ 값을 μ²˜λ¦¬ν•  λ•Œ μœ μš©ν•˜λ‹€.

function triple(value:number | string){
    if(typeof value === "string"){
        return value.repeat(3);
    }
    return value * 3;
}

2. Truthiness κ°€λ“œ


참인지 거짓인지 κ²€μ‚¬ν•΄μ„œ νƒ€μž… 쒁히기λ₯Ό ν•  수 μžˆλ‹€.

const printLetters = (word?: string) => {
    if(word){
        for(let char of word){
            console.log(char);
        }
    }else{
        console.log('not Word')
    }
}

3. Equality(동등) 쒁히기


비ꡐ μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ—¬ νƒ€μž…μ„ νŒλ³„ν•˜κ³  쒁히기λ₯Ό ν•  수 μžˆλ‹€.

function seomDemo(x: string | number, y: string | boolean){
    if(x === y){
        x.toUpperCase();
    }
}

3. In μ—°μ‚°μžλ‘œ 쒁히기


inμ—°μ‚°μžλ₯Ό 톡해 νƒ€μž…μ„ 쒁힐수 있으며 λŒ€λΆ€λΆ„μ˜ κ²½μš°μ—λŠ” μΈν„°νŽ˜μ΄μŠ€λ‚˜ νƒ€μž… 별칭을 μ‚¬μš©ν•΄ 객체둜 μž‘μ—…ν•˜κΈ° λ•Œλ¬Έμ— ν”ν•˜κ²Œ μ‚¬μš©λœλ‹€.
interface Movie{
    title: string;
    duration: number;
}

interface TVShow{
    title: string;
    numEpisodes: number;
    episodeDuration: number;
}

function getRuntime(media: Movie|TVShow){
    if("numEpisodes" in media){
        return media.numEpisodes * media.numEpisodes;
    }
    return media.duration;
}

5. Instanceof 쒁히기


InstanceofλŠ” ν•΄λ‹Ή ν”„λ‘œν† νƒ€μž… 체인 내에 μ‘΄μž¬ν•˜λŠ”μ§€λ₯Ό ν™•μΈν•˜λŠ” μ—°μ‚°μžμ΄λ‹€. νŠΉμ • 클래슀의 μΈμŠ€ν„΄μŠ€μ— 값이 μ‘΄μž¬ν•˜λŠ”μ§€ 등을 확인할 수 μžˆλ‹€.
function printFullDate(date: string | Date){
    if (date instanceof Date){
        console.log(date.toUTCString());
    }else{
        console.log(new Date(date).toUTCString());
    }
}

class User{
    constructor(public name: string){}
}

class Company{
    constructor(public name: string){}
}

function printName(entity: User | Company){
    if(entity instanceof User ){
        entity // User
    }else{
        entity // Company
    }
}

typeofλ₯Ό μ‚¬μš©ν•  수 μ—†λŠ” κ²½μš°μ— instanceofλ₯Ό μ‚¬μš©ν•˜κ²Œ λ˜λŠ”λ° κ·ΈλŸ¬ν•œ κ²½μš°λŠ” new ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ μ΄ˆκΈ°ν™”ν•œ ν΄λž˜μŠ€λ“±μœΌλ‘œ μž‘μ—…μ„ ν•˜λŠ” 경우 등이 μžˆλ‹€.

6. νƒ€μž… λͺ…μ œ(Predicate) 닀루기


parameterName is Type ν˜•μ‹μœΌλ‘œ μ‚¬μš©λœλ‹€.

interface Cat{
    name: string;
    numberLives: number;
}

interface Dog{
    name: string;
    breed: string;
}

// νƒ€μž… λͺ…μ œλŠ” isXX ν˜•μ‹μ˜ μ΄λ¦„μœΌλ‘œ ν•˜λŠ”κ²ƒμ΄ μΌλ°˜μ μ΄λ‹€.
function isCat(animal: Cat | Dog): animal is Cat{ // 이 ν•¨μˆ˜κ°€ trueλ₯Ό λ°˜ν™˜ν•˜λ©΄ μ „λ‹¬λœ 값이 Cat νƒ€μž…μ΄λΌλŠ” λœ»μ΄λ‹€.
    return (animal as Cat).numberLives !== undefined;
}

function makeNoise(animal: Cat | Dog): string{
    if(isCat(animal)){
        return "Meow"
    }else{
        return "wowo"
    }
}

7. νŒλ³„ μœ λ‹ˆμ˜¨


νƒ€μž… νŒλ³„μ— λ„μ›€λ˜λŠ” νŒ¨ν„΄μΈλ° 곡톡적인 ν”„λ‘œνΌν‹°λ₯Ό κ³΅μœ ν•˜λŠ” μ—¬λŸ¬ μœ ν˜•μ„ μƒμ„±ν•˜κ³  ν•΄λ‹Ή ν”„λ‘œνΌν‹°κ°€ λ¦¬ν„°λŸ΄ νƒ€μž… 즉 λ¦¬ν„°λŸ΄ 값이 λ˜λŠ” 것이닀.
λͺ¨λ“  νƒ€μž…μ˜ κ³΅ν†΅λœ ν”„λ‘œνΌν‹°μ— νŒλ³„μžλ₯Ό μΆ”κ°€ν•œλ‹€.


interface Rooster{
    name : string;
    weight: number;
    age: number;
    kind: "rooster"; // νŒλ³„μž ν”„λ‘œνΌν‹°
}

interface Cow{
    name : string;
    weight: number;
    age: number;
    kind: "cow"; // νŒλ³„μž ν”„λ‘œνΌν‹°
}

interface Pig{
    name : string;
    weight: number;
    age: number;
    kind: "pig"; // νŒλ³„μž ν”„λ‘œνΌν‹°
}

type FarmAnimal = Pig | Rooster | Cow;

function getFarmAnimalSound(animal: FarmAnimal){
    switch(animal.kind){
        case("pig"):
            return "Oink"
        case("cow"):
            return "Moooo"
        case("rooster"):
            return "Cockadoodle"
    }
}

8. μ†Œμ§„κ²€μ‚¬(Exhaustiveness check) 와 Never


였λ₯˜ 및 μƒνƒœμ½”λ“œλ₯Ό ν™•μ‹€ν•˜κ²Œ μ²˜λ¦¬ν•  λ•Œ μ‚¬μš©ν•œλ‹€.
never νƒ€μž…μ€ μ–΄λ””λ“  ν• λ‹Ήλ˜μ§€λ§Œ neverμ—λŠ” μ–΄λ–€ νƒ€μž…λ„ ν• λ‹Ήν•  수 μ—†λ‹€

밑에 μ½”λ“œλŠ” neverκ°€ μžˆμ–΄μ„œ defaultλŠ” μ ˆλŒ€ trueκ°€ 되면 μ•ˆλœλ‹€.


interface Rooster{
    name : string;
    weight: number;
    age: number;
    kind: "rooster"; // νŒλ³„μž ν”„λ‘œνΌν‹°
}

interface Cow{
    name : string;
    weight: number;
    age: number;
    kind: "cow"; // νŒλ³„μž ν”„λ‘œνΌν‹°
}

interface Pig{
    name : string;
    weight: number;
    age: number;
    kind: "pig"; // νŒλ³„μž ν”„λ‘œνΌν‹°
}

interface Sheep{
    name: string;
    weight: number;
    age: number;
    kind: "sheep";
}
type FarmAnimal = Pig | Rooster | Cow | Sheep;

function getFarmAnimalSound(animal: FarmAnimal){
    switch(animal.kind){
        case("pig"):
            return "Oink"
        case("cow"):
            return "Moooo"
        case("rooster"):
            return "Cockadoodle"
        default:
            // neverνƒ€μž…μ΄ μžˆλŠ”λ° defaultκ°€ trueκ°€ λ˜μ–΄μ„œ 였λ₯˜κ°€ λ°œμƒν•œλ‹€.
            const _exhaustiveCheck: never = animal; // error : Type 'Sheep' is not assignable to type 'never' 
            return _exhaustiveCheck;
    }
}
profile
Brendan Eich, Jordan Walke, Evan You, κΆŒμš©μ€€

0개의 λŒ“κΈ€