๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ‘จ‍๐Ÿ’ป web.dev/js.ts

[TypeScript] enum ๊ณผ union type, ์–ธ์ œ ์จ์•ผํ• ๊นŒ?

by HandHand 2022. 7. 9.

 

๐Ÿ“Œ TypeScript ์—์„œ ์—ฐ๊ด€๋œ ํƒ€์ž… ์ •์˜ํ•˜๊ธฐ

TypeScript ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด ์„œ๋กœ ์—ฐ๊ด€๋œ ๊ฐ’์„ ํƒ€์ž…์œผ๋กœ ์ •์˜ํ•  ๋•Œ

enum ํ˜น์€ union type ์„ ์ด์šฉํ•ด์„œ ์ •์˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘์—์„œ ์–ด๋–ค๊ฒƒ์ด ์ตœ์„ ์ธ์ง€ ํ•ญ์ƒ ๊ณ ๋ฏผ์ด ๋˜์—ˆ๋Š”๋ฐ

์ด๋ฒˆ ๊ธฐํšŒ์— ๋‘˜์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ •๋ฆฌํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๋””๋ฐ”์ด์Šค์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•œ ํƒ€์ž…์„ string union ์œผ๋กœ ์ •์˜ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

type DeviceType = "phone" | "desktop" | "pad" | "watch"

 

์ด๋ฅผ enum ์„ ์ด์šฉํ•ด์„œ ํƒ€์ž…์„ ์ •์˜ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

enum DeviceType {
    phone = 'phone',
    desktop = 'desktop',
    tablet = 'tablet',
    watch = 'watch'
}

 

๋‘ ์ข…๋ฅ˜์˜ ๋ฐฉ์‹์œผ๋กœ ํƒ€์ž…์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ..

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ค ๊ฒฝ์šฐ์— ์ด ๋‘ ๋ฐฉ๋ฒ•์ค‘์—์„œ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

 

๐Ÿ“Œ enum ์˜ ๋ฌธ์ œ์ 

๋จผ์ € enum ์„ ์‚ฌ์šฉํ•  ๋•Œ ์–ด๋–ค ์ œ์•ฝ์‚ฌํ•ญ์ด ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์–ด๋–ค ๊ฒฝ์šฐ์— enum ์„ ์‚ฌ์šฉํ•ด์•ผํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ๋„์›€์ด ๋˜์ง€ ์•Š์„๊นŒ์š”?

 

1๏ธโƒฃ enum ํ™•์žฅํ•˜์—ฌ ํƒ€์ž… ์ •์˜ํ•˜๊ธฐ

enum ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ™•์žฅ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ ํŠน์ • enum ๊ณผ ๋‹ค๋ฅธ enum ์„ ํ•ฉ์ณ ๋‹ค๋ฅธ enum ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š”

๋‹ค์Œ๊ณผ ๊ฐ™์ด union type ์„ ์ด์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

enum Fruit {
    APPLE,
    BANANA
}

enum AnotherFruit {
    PINEAPPLE,
    GRAPE,
}

type Fruits = Fruit | AnotherFruit

const fruit: Fruit = Fruit.APPLE

2๏ธโƒฃ enum ์— ๋Œ€ํ•œ import ํ•„์š”์„ฑ

enum ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด value ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์ง์ ‘ ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋Œ€์‹  ํ•ด๋‹นํ•˜๋Š” enum ์„ ์‚ฌ์šฉํ•˜๋Š” ์ชฝ์—์„œ ๋ถˆ๋Ÿฌ์™€ ํ• ๋‹นํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ union type ์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด IDE ์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ์˜๋ฆฌํ•˜๊ฒŒ ํƒ€์ž…์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์–ด

๊ฐ€๋”์”ฉ์€ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์ด ๋ฒˆ๊ฑฐ๋กญ๋‹ค๊ณ  ๋Š๊ปด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

/** 
 * ๋งŒ์•ฝ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Fruit ์ด๋ž€ enum ์ด ์ •์˜๋˜์–ด์žˆ๊ณ 
 */
enum Fruit {
    APPLE = 'APPLE',
    BANANA = 'BANANA'
}

/**
 * ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์ด enum ์„ ์ธ์ž๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด
 */
getSomeFruits('APPLE') // โŒ
getSomeFruits(Fruit.APPLE) // โœ…

3๏ธโƒฃ enum ์˜ ๋ถˆ์™„์ „ํ•œ ํƒ€์ž… ์•ˆ์ •์„ฑ

enum ์ค‘์—์„œ numeric enum ์€ ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•ด์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด๋Š” numeric enum ์„ ํ™œ์šฉํ•ด์„œ flag ๊ฐ’์„ ์ •์˜ํ•˜๊ณ 

bitwise ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ฐ’์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•œ ์„ค๊ณ„๋ฐฉ์‹์— ๊ธฐ์ธํ•œ ๋™์ž‘์ž…๋‹ˆ๋‹ค.

Github ์—์„œ TypeScript repository ์˜ ๊ด€๋ จ ์ด์Šˆ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ํ•ด๋‹น ๋ฐฉ์‹์— ๋Œ€ํ•œ ๊ทผ๊ฑฐ๋ฅผ ์งš์–ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

@from DanielRosenwasser

 

The behavior is motivated by bitwise operations.
There are times when Flag.Foo | Flag.Bar is intended to produce another Flag.
Instead you end up with number, and you don't want to have to cast back to Flag.

 

 

enum Status {
    pending = 0,
    success = 1,
    fail = 2
}

const newStatus: Status = 100 // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ์•ˆํ•จ!

 

์œ„์™€ ๊ฐ™์ด Status ๋ฅผ ์ •์˜ํ–ˆ์„ ๋•Œ, 0, 1, 2 ๋งŒ ํ• ๋‹น ๊ฐ€๋Šฅํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ

์˜๋„ํ•˜์ง€ ์•Š์€ ๊ฐ’์ธ 100 ์„ ํ• ๋‹นํ•ด๋„ ์•„๋ฌด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ์—†์ด ์ปดํŒŒ์ผ ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ numeric enum ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

4๏ธโƒฃ ์ฝ”๋“œ ์‚ฌ์ด์ฆˆ ์ฆ๊ฐ€

enum ์€ ๊ฒฐ๊ตญ JavaScript ๊ฐ์ฒด๋กœ ์ปดํŒŒ์ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ enum ์„ ์–ธ์€

์ „์ฒด์ ์ธ ์ฝ”๋“œ ์‚ฌ์ด์ฆˆ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Š” ๋ฐ”๋กœ ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ์‹ค์ œ ๋ฒˆ๋“ค๋ง ์‚ฌ์ด์ฆˆ ๋น„๊ต๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ ๋ฒˆ๋“ค๋ง๋œ ์ฝ”๋“œ ์‚ฌ์ด์ฆˆ ๋น„๊ตํ•˜๊ธฐ

์‹ค์ œ ๋ฒˆ๋“ค๋ง๋˜๋Š” ์ฝ”๋“œ ์‚ฌ์ด์ฆˆ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ webpack + typescript ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜๊ณ 

DeviceType ์„ ์ด์šฉํ•ด ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

1๏ธโƒฃ enum

/** โœ… example.ts ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž…์ด ์ •์˜๋˜์–ด์žˆ๊ณ  */

export enum DeviceType {
  phone = "phone",
  desktop = "desktop",
  tablet = "tablet",
  watch = "watch",
}

/** โœ… index.ts ์—์„œ ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด? */

import { DeviceType } from "./example";

const device: DeviceType = DeviceType.desktop;

/** โœ… ๋‹ค์Œ๊ณผ ๊ฐ™์ด bundle.js ๊ฐ€ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค! */

/***/ "./src/example.ts":
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
exports.DeviceType = void 0;
var DeviceType;
(function (DeviceType) {
    DeviceType["phone"] = "phone";
    DeviceType["desktop"] = "desktop";
    DeviceType["tablet"] = "tablet";
    DeviceType["watch"] = "watch";
})(DeviceType = exports.DeviceType || (exports.DeviceType = {}));

/***/ }),

/***/ "./src/index.ts":
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var example_1 = __webpack_require__(/*! ./example */ "./src/example.ts");
var device = example_1.DeviceType.desktop;

enum bundle size

2๏ธโƒฃ const enum

/** โœ… example.ts ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž…์ด ์ •์˜๋˜์–ด์žˆ๊ณ  */

export const enum DeviceType {
  phone = "phone",
  desktop = "desktop",
  tablet = "tablet",
  watch = "watch",
}

/** โœ… index.ts ์—์„œ ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด? */

import { DeviceType } from "./example";

const device: DeviceType = DeviceType.desktop;

/** โœ… ๋‹ค์Œ๊ณผ ๊ฐ™์ด bundle.js ๊ฐ€ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค! */

/***/ "./src/index.ts":
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var device = "desktop" /* desktop */;

/***/ })

const enum bundle size

3๏ธโƒฃ const object

๋‹ค์Œ๊ณผ ๊ฐ™์ด Device ์— ๋Œ€ํ•œ ํƒ€์ž…์„ const object ๋กœ ์ •์˜ํ•œ๋‹ค๋ฉด,

/** โœ… example.ts ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž…์ด ์ •์˜๋˜์–ด์žˆ๊ณ  */

export const Device = {
  phone: "phone",
  desktop: "desktop",
  tablet: "tablet",
  watch: "watch",
} as const;

export type DeviceType = typeof Device[keyof typeof Device];

/** โœ… index.ts ์—์„œ ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด? */

import { Device, DeviceType } from "./example";

const device: DeviceType = Device.desktop;

/** โœ… ๋‹ค์Œ๊ณผ ๊ฐ™์ด bundle.js ๊ฐ€ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค! */

/***/ "./src/example.ts":
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
exports.Device = void 0;
exports.Device = {
    phone: "phone",
    desktop: "desktop",
    tablet: "tablet",
    watch: "watch",
};

/***/ }),

/***/ "./src/index.ts":
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var example_1 = __webpack_require__(/*! ./example */ "./src/example.ts");
var device = example_1.Device.desktop;

/***/ })

const object bundle size

4๏ธโƒฃ union type

/** โœ… example.ts ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž…์ด ์ •์˜๋˜์–ด์žˆ๊ณ  */

export type DeviceType = "phone" | "desktop" | "tablet" | "watch";

/** โœ… index.ts ์—์„œ ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด? */

import { DeviceType } from "./example";

const device: DeviceType = "desktop";

/** โœ… ๋‹ค์Œ๊ณผ ๊ฐ™์ด bundle.js ๊ฐ€ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค! */

/***/ "./src/index.ts":
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var device = "desktop";

/***/ })

union type bundle size

5๏ธโƒฃ ์ข…ํ•ฉํ•ด๋ณด๋ฉด..

์œ„ ๋ฒˆ๋“ค๋ง ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•ด๋ณด๋ฉด,

union type < const enum < const object < enum ์ˆœ์œผ๋กœ ์ฝ”๋“œ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉฐ

ํƒ€์ž…์ด ๋งŽ์•„์งˆ ์ˆ˜๋ก ์ „์ฒด ์ฝ”๋“œ ๋ฒˆ๋“ค๋ง ์‚ฌ์ด์ฆˆ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ enum ์€ ๋Œ€์ฒด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

1๏ธโƒฃ union type ์‚ฌ์šฉํ•˜๊ธฐ

string enum ์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด union type ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋‘ ํƒ€์ž…์˜ ๊ธฐ๋Šฅ์ด ๋™์ผํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์„ธ ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ชฉ์ ์—์„œ ๋™์ผํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

numeric enum ์€ ์•ž์„œ ๋งํ•œ ์ด์œ ๋“ค ๋•Œ๋ฌธ์— ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๋ฏ€๋กœ

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋Š” union type ์œผ๋กœ ์ •์˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

enum Permission {
    READ = 'READ',
    WRITE = 'WRITE',
    EXECUTE = 'EXECUTE'
}

type Permission = 'READ' | 'WRITE' | 'EXECUTE'

2๏ธโƒฃ const object ์‚ฌ์šฉํ•˜๊ธฐ

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด, ๋ชจ๋˜ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” enum ์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ 

๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

const Device = {
    phone: 'phone',
    desktop: 'desktop',
    tablet: 'tablet',
    watch: 'watch'
} as const

// type DeviceType = "phone" | "desktop" | "tablet" | "watch"
type DeviceType = typeof Device[keyof typeof Device]

๐Ÿ“Œ enum ์‚ฌ์šฉ์„ ์ œํ•œํ•˜๋Š” eslint ๊ทœ์น™

eslint ์˜ no-restricted-syntax ๊ทœ์น™์„ ์ด์šฉํ•˜๋ฉด enum ์‚ฌ์šฉ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํŒ€ ๋‚ด์—์„œ enum ์‚ฌ์šฉ์„ ์ œํ•œํ•˜๋„๋ก ๊ฒฐ์ •ํ–ˆ๋‹ค๋ฉด ๊ณ ๋ คํ•ด๋ณผ๋งŒํ•œ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

{
  "rules": {
    "no-restricted-syntax": [
      "error",
      {
        "selector": "TSEnumDeclaration",
        "message": "โŒ do not use enum"
      }
    ]
  }
}

 

๐Ÿ“Œ ๊ทธ๋ ‡๋‹ค๋ฉด ์–ธ์ œ enum ์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ enum ์„ ์œ„ ๋‘ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋Œ€์ฒด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ค ๊ฒฝ์šฐ์— enum ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ์š”?

 

๋ฆฌ๋ฒ„์Šค ๋งคํ•‘ (reverse mapping)

enum ์˜ ํŠน๋ณ„ํ•œ ์ ์€ numeric enum ์— ๋Œ€ํ•ด์„œ reverse mapping ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

// ๋‹ค์Œ๊ณผ ๊ฐ™์€ enum ์€
enum Animal {
    DOG,
    CAT,
}

// ์•„๋ž˜์™€ ๊ฐ™์ด ์ปดํŒŒ์ผ๋ฉ๋‹ˆ๋‹ค
var Animal;
(function (Animal) {
    Animal[Animal["DOG"] = 0] = "DOG";
    Animal[Animal["CAT"] = 1] = "CAT";
})(Animal || (Animal = {}));

 

์—ฌ๊ธฐ์„œ Animal['DOG'] = 0 ๊ณผ ๋™์‹œ์— ๋ฐ˜๋Œ€๋กœ Animal[0] = 'DOG' ๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„

reverse mapping ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

reverse mapping ์€ enum ๋งŒ์˜ ํŠน์ง•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ..

์‚ฌ์‹ค reverse mapping ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์‹ค์ œ๋กœ ๋งŽ์„๊นŒ? ํ•˜๋Š” ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค.

 

๋Ÿฐํƒ€์ž„์— ํƒ€์ž…์— ์ ‘๊ทผํ•ด์„œ ๊ฐ’์„ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์ด ๊ณผ์—ฐ ์ข‹์€ ์„ค๊ณ„๊ฐ€ ๋งž์„์ง€,

๊ทธ๋ฆฌ๊ณ  ์„ค๋ น ๋ฐ˜๋“œ์‹œ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ํ•„์š”ํ•˜๋”๋ผ๋„ ์ด๋ฅผ enum ์ด ์•„๋‹ˆ๋ผ ๋ณ„๋„์˜ ๊ฐ์ฒด๋กœ ์ •์˜ํ•ด์„œ

์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋งž์ง€ ์•Š์„๊นŒ? ๋ผ๋Š” ๊ณ ๋ฏผ์ด ๋“ญ๋‹ˆ๋‹ค. ๐Ÿค”

 

โš ๏ธ numeric enum ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ

๊ทธ๋ฆฌ๊ณ  numeric enum ์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ’์„ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด

0 ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€ํ•œ ๊ฐ’์ด ์ž๋™์œผ๋กœ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

์ด ๋•Œ๋ฌธ์— ์ถ”ํ›„์— ํ•„๋“œ์˜ ์„ ์–ธ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ๋˜๋ฉด ๊ฐ’์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

enum Animal {
    DOG, // DOG = 0
    CAT, // CAT = 1
}

// versus.

enum Animal {
    CAT, // CAT = 0
    DOG, // DOG = 1 
}

 

๊ทธ ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•œ ์ฝ”๋”ฉ์„ ์œ„ํ•ด ๋ช…์‹œ์ ์œผ๋กœ ์ •์ˆ˜๊ฐ’์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜๋Š”๋ฐ,

Flag ๊ฐ’์„ ์ด์šฉํ•˜๋Š” ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ๋ƒฅ string enum ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด

๋” ์ง๊ด€์ ์ด๊ณ  ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•์ด ์•„๋‹๊นŒ? ํ•˜๋Š”๊ฒŒ ์ €์˜ ๊ฐœ์ธ์ ์ธ ์˜๊ฒฌ์ž…๋‹ˆ๋‹ค. ๐Ÿค”

 

๐Ÿ“Œ ๊ทธ๋ž˜์„œ ๊ฒฐ๋ก ์€?

TypeScript ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ ํƒ€์ž… ์„ ์–ธ์— ๋Œ€ํ•œ ์ผ์ •ํ•œ ๊ทœ์น™์ด ์žˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•ด

์œ„์—์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ๋“ค๊ณผ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋“ค์˜ ์˜๊ฒฌ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒฐ๋ก ์„ ๋‚ด๋ ธ์Šต๋‹ˆ๋‹ค.

  1. ์šฐ์„  union type ์œผ๋กœ ํƒ€์ž…์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋งŒ์•ฝ ๋Ÿฐํƒ€์ž„์— ์ ‘๊ทผ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด const object ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ฆฌ๋ฒ„์Šค ๋งคํ•‘ ๋“ฑ ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ์— ํ•œํ•˜์—ฌ enum ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“Œ ์ฐธ๊ณ ์ž๋ฃŒ

Handbook - Enums

Union Types vs. Enums in TypeScript

The Difference Between TypeScript Unions, Enums, and Objects

Typescript has unions, so are enums redundant?

 

 

๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€