Skip to Content
Sunbeen's Blog
Documents๊ตฌ TypescriptBasic_Grammer03. ๐ŸŸฆ enum, const enum, as const, keyof, typeof ์ •๋ฆฌ

03. ๐ŸŸฆ enum, const enum, as const, keyof, typeof ์ •๋ฆฌ

๐Ÿ“Œ 1. Enum (์—ด๊ฑฐํ˜•)

์—ฌ๋Ÿฌ ๊ด€๋ จ๋œ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ์ด๋ฆ„ ์•„๋ž˜์— ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋Šฅ

  • ๊ธฐ๋ณธ๊ฐ’์€ ์ˆซ์ž 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ 1์”ฉ ์ฆ๊ฐ€
  • ์ˆซ์ž ์—ด๊ฑฐํ˜•: enum Direction { Up = 0, Down = 1 ... } ์™€ ๋™์ผ
  • ๋Ÿฐํƒ€์ž„์—๋„ ๊ฐ์ฒด๋กœ ์กด์žฌํ•˜๋ฉฐ, ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์ด ๊ฐ€๋Šฅํ•จ
//Enum Default enum EDirection { Up, // 0 Down, // 1 Left, // 2 Right // 3 } // ์–‘๋ฐฉํ–ฅ ๋งตํ•‘ EDirection.Up; // 0 EDirection[0]; // 'Up'

๐Ÿ“Œ 2. const enum

๋Ÿฐํƒ€์ž„์—๋Š” ์ œ๊ฑฐ๋˜๊ณ , ์ปดํŒŒ์ผ ์‹œ ์ˆซ์ž ๊ฐ’์œผ๋กœ ์ธ๋ผ์ธ๋จ (์ตœ์ ํ™”)

โ—์ฃผ์˜

  • const enum์€ ๊ฐ์ฒด๊ฐ€ ๋Ÿฐํƒ€์ž„์— ์ƒ์„ฑ๋˜์ง€ ์•Š์Œ
  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ tsconfig์˜ preserveConstEnums ์˜ต์…˜์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜, ๋Œ€์‹  ๊ฐ์ฒด + as const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ

๐Ÿ“Œ 3. ๋ฌธ์ž์—ด ์—ด๊ฑฐํ˜•

  • ๊ฐ’์ด ๋ฌธ์ž์—ด์ด๋ฉด ์ž๋™ ์ฆ๊ฐ€ ์—†์Œ, ๋ชจ๋“  ๊ฐ’ ์ˆ˜๋™ ์ง€์ • ํ•„์š”
  • JS๋กœ ๋ณ€ํ™˜ ์‹œ const enum์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด ์ƒ์ˆ˜๋งŒ ๋‚จ์Œ
const enum Direction { Up = "UP", Down = "DOWN" }

๐Ÿ“Œ 4. ๊ฐ์ฒด๋ฅผ enum์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ (with as const)

  • as const๋Š” ๊ฐ์ฒด ์†์„ฑ๋“ค์„ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์œผ๋กœ ๊ณ ์ •ํ•จ (readonly + ์ •ํ™•ํ•œ ๊ฐ’)
  • ๋Ÿฐํƒ€์ž„์—๋„ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋””๋ฒ„๊น…, ์ถœ๋ ฅ ๋“ฑ์— ์œ ๋ฆฌ
  • ํƒ€์ž… ์ถ”๋ก  ๋ฐ ํ™œ์šฉ์„ฑ์ด ์ข‹๊ธฐ ๋•Œ๋ฌธ์— ์‹ค๋ฌด์—์„œ ๋” ๋งŽ์ด ์‚ฌ์šฉ
  • as const ๋Š” ๋ชจ๋“  ๊ฐ’์„ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์œผ๋กœ ๊ณ ์ • + Read Only
const ODirection = { Up: 0, Down: 1, Left: 2, Right: 3 } as const;
// ํƒ€์ž… ์ถ”๋ก ์‹œ, number๋กœ ์‚ฌ์šฉ const enum EDirection { UP, //0 Down, //1 Left, //2 Right //3 } // // JS๋กœ ๋ณ€ํ™˜ ์‹œ โ†’ const a = 0; const a = EDirection.Up; const b = EDirection.Down;

๐Ÿ“Œ 5. ํƒ€์ž… ์ง€์ •

Enum์˜ ํƒ€์ž…์„ ์ง์ ‘์ •์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅ

const enum EDirection {UP : 0, Down :1, Left : 2, Right : 3,} { UP = 0, Down =1, Left = 2, Right = 3, } //Enum์€ ์ง์ ‘ ํ•จ์ˆ˜๋กœ์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ function walk(dir: EDirection) {}

๐Ÿ“Œ 6. ํƒ€์ž… ์ถ”๋ก 

  • typeof : ๊ฐ’์„ ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๋Š” ํ‚ค์›Œ๋“œ
  • keyof : keyof: ๊ฐ์ฒด ํƒ€์ž…์˜ โ€œํ‚คโ€๋งŒ ์ถ”์ถœํ•˜์—ฌ ์œ ๋‹ˆ์˜จ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜
const obj = { a: "apple", b: "banana" }; type ObjType = typeof obj; // ObjType { a: string; b: string; } type Keys = keyof ObjType; // "a" | "b" // ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” obj๊ฐ€ '๊ฐ’'์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ keyof obj ๋Š” โŒ ์˜ค๋ฅ˜ type Key = keyof typeof obj; // โ†’ typeof obj: { a: string; b: string } // โ†’ keyof { a: string; b: string } โ†’ "a" | "b" type Key = (typeof obj)[keyof typeof obj]; // typeof obj[keyof typeof obj] โ†’ string ๊ทธ ํƒ€์ž…์˜ ๊ฐ’์˜ ํƒ€์ž… (์œ ๋‹ˆ์˜จ) // string | string | string โ†’ string
const Direction = { Up: 0, Down: 1, Left: 2, Right: 3, } as const; typeof Direction { readonly Up: 0, readonly Down: 1, readonly Left: 2, readonly Right: 3 } // ํ‚ค ํƒ€์ž… ์ถ”๋ก  // keyof typeof Direction type DirectionKey = keyof typeof Direction // => 'Up' | 'Down' | 'Left' | 'Right' // ๊ฐ’ ํƒ€์ž… ์ถ”๋ก  // (typeof Direction)[keyof typeof Direction] // โ†’ ์œ„ ํ‚ค๋“ค๋กœ ํ•ด๋‹น ๊ฐ์ฒด ํƒ€์ž…์„ ์ธ๋ฑ์‹ฑํ•˜๋ฉด ๊ฐ’๋“ค์˜ ํƒ€์ž… ์œ ๋‹ˆ์˜จ์ด ๋ฉ๋‹ˆ๋‹ค. type DirectionValue = (typeof Direction)[keyof typeof Direction] // as const๊ฐ€ ๋ถ™์–ด์žˆ์„ ๊ฒฝ์šฐ => 0 | 1 | 2 | 3 // ์—†์œผ๋ฉด number
//Enum ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ์„๋•Œ //Value type Direction = (typeof ODirection)[keyof typeof ODirection] as const // Key type Direction = [keyof typeof ODirection] as const function run(dir: Direction) {} walk(EDirection.Left) run(EDirection.Right)
Last updated on