두 개 이상의 잠재적 타입이 될 수 있는 값을 다루기 위해 유니언 타입과 내로잉을 살펴봤으니
지금부터는
리터럴 타입리터럴 타입
literal
type
을 소개하겠습니다. 리터럴 타입은 좀 더 구체적인 버전의 원시
타입입니다.
다음
philosopher
변수를 보겠습니다.
const philosopher = "Hypatia";
philosopher
는 어떤 타입인가요? 얼핏 봐도
string
타입이라고 말할 수 있고 실제로도
string
타입입니다.
하지만
philosopher
는 단지
string
타입이 아닌
"
Hypatia
"
라는 특별한 값입니다. 따라서
변수
philosopher
의 타입은 기술적으로 더 구체적인
"
Hypatia
"
입니다.
이것이 바로 리터럴 타입의 개념입니다. 원시 타입 값 중 어떤 것이 아닌
특정 원싯값특정 원싯값
으로 알려
진 타입이 리터럴 타입니다. 원시 타입
string
은 존재할 수 있는 모든 가능한 문자열의 집합을
나타냅니다. 하지만 리터럴 타입인
"
Hypatia
"
는 하나의 문자열만 나타냅니다.
만약 변수를 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.