[ts] Literal, Union, Intersection Types

Lee Tae-Sung·2022년 12월 24일
0

ts

목록 보기
4/7

1. Literal Types

=> const는 값을 변경할 수 없기 때문에 "Tom"으로 특정됨
=> let은 바꿀 수 있기에 string으로

2. 식별가능한 Union Type


interface Car {
    name: 'car';
    color: string;
    start(): void;
}

interface Mobile {
    name: 'mobile';
    color: string;
    call(): void;
}

function getGift(gift: Car | Mobile) {
    console.log(gift.color);
    if (gift.name === "car") {
        gift.start();
    } else {
        gift.call();
    }
}

let newG: Car = {
    name: 'car',
    color: 'black',
    start() {
        console.log('드더');
    },
}

getGift(newG);

3. Intersection Types

interface Car {
    name: string;
    start(): void;
}

interface Toy {
    name: string;
    color: string;
    price: number;
}

const toyCar: Toy & Car = {
    name: '타요',
    start() {},
    color: "blue",
    price: 1000,
}
profile
긍정적인 에너지를 가진 개발자, 이태성입니다.

0개의 댓글