■ enum
const enum EDirection {
Up, // 위에서부터 0,1,2,3... 의 값을 가진다.
Down, // Down = 'hello' 등의 문자열을 값으로 지정해줄 수도 있다.
Left,
Right,
}
const a = EDirection.Up; // a = 0
const b = EDirection.Left; // b = 2
// 위 enum 정의와 같은 표현
const ODirection = {
Up: 0,
Down: 1,
Left: 2,
Right: 3,
} as const; // 위 값을 상수로 쓰겠다는 표현. readonly로 값 수정 불가
const d = ODirection.Up; // d = 0
■ keyof , typeof
const obj = { a: "1", b: "2", c: "3" } as const; // as const 없으면 obj 타입은 string으로 추론
type thisKey = keyof typeof obj; // "a" | "b" | "c"
type thisValue = typeof obj[keyof typeof obj] // "1" | "2" | "3"
'Language > TypeScript' 카테고리의 다른 글
타입과 인터페이스 상속 (0) | 2023.02.11 |
---|---|
union(|)과 intersection(&) (0) | 2023.02.09 |
원시 래퍼 타입, 템플릿 리터럴 타입, rest, 튜플 (0) | 2023.02.05 |
never 타입과 느낌표 (0) | 2023.02.04 |
JS 변환 시 사라지는 부분 (0) | 2023.02.02 |