[Typescript] 리터럴 타입

Asher Park·2023년 1월 18일
0
post-thumbnail

스파르타코딩클럽 내일배움캠프 Typescript 강의를 들으며 공부한 것을 적은 것입니다.

리터럴 타입 (Literal)

  • 집합 타입의 구체적인 하위 타입
  • 문자열과 숫자, 두가지 리터럴 타입이 있다

문자열 리터럴 타입

type Easing = "ease-in" | "ease-out";

class UI {
	animate(easing: Easing) {
    	if (easing === "ease-in") {
        	...
        } else if (easing === "ease-out") {
        	...
        } else {
        	// 타입을 무시하면 도달하는 곳
        }
    }
}

숫자형 리터럴 타입

function rollDice(): 1|2|3|4|5|6 {
	return (Math.floor(Math.random() * 6 + 1)
            as 1|2|3|4|5|6;
}
profile
배움에는 끝이없다

0개의 댓글