TIL: JS, TS | 리터럴(Literal) - 220909

Lumpen·2022년 9월 10일
0

TIL

목록 보기
144/242

리터럴

리터럴이란 코드의 고정된 데이터(값)를 대표하는 용어다
고정된 값을 표현하는 방식을 뜻하는 것으로

ex) 정수 리터럴: 자바스크립트가 표현할 수 있는 모든 정수

const a = 1; // 정수 리터럴
let b = 'str'; // 문자열 리터럴
const c = []; // [] = 배열 리터럴
const d = {}; // {} = 객체 리터럴

객체를 생성하는 방법 중 객체 리터럴이라고 하면
객체 자체를 변수에 할당해서 생성하는 것..

리터럴 타입

리터럴 타입은 집합의 타입보다 구체적인 하위 타입을 말한다

const a: 1 = 1
const b: 'b' = 'b'

template literal type

Template Literal Type이란
기존 TypeScript의 String Literal Type을 기반으로
새로운 타입을 만드는 도구로
코드에 안정성을 더해준다

basic

type Toss = 'toss';

// type TossPayments = 'toss payments';
type TossPayments = `${Toss} payments`;

union type

type Toss = 'toss';
type Companies = 'core' | 'bank' | 'securities' | 'payments' | 'insurance';

// type TossCompanies = 'toss core' | 'toss bank' | 'toss securities' | ...;
type TossCompanies = `${Toss} ${Companies}`

union types

type VerticalAlignment = "top" | "middle" | "bottom";
type HorizontalAlignment = "left" | "center" | "right";

// type Alignment =
//   | "top-left"    | "top-center"    | "top-right"
//   | "middle-left" | "middle-center" | "middle-right"
//   | "bottom-left" | "bottom-center" | "bottom-right"
type Alignment = `${VerticalAlignment}-${HorizontalAlignment}`;

토스 테크 블로그

profile
떠돌이 생활을 하는. 실업자는 아니지만, 부랑 생활을 하는

0개의 댓글