Ver3.0 ๐ฏ ํ์ ์คํฌ๋ฆฝํธ ์ด๊ฑฐํ Enum๊ณผ const assertion์ ํตํ ์์ ๊ด๋ฆฌ

๐ฏ ํ์ ์คํฌ๋ฆฝํธ ์ด๊ฑฐํ Enum๊ณผ const assertion์ ํตํ ์์ ๊ด๋ฆฌ
์น๊ตฌ์ผ, ํ์ ์คํฌ๋ฆฝํธ๋ก ์์ ๊ด๋ฆฌํ๋ ๋ฒ ์ ๋๋ก ์๋ ค์ค๊ฒ! ๐
๐ค ์ ์์ ๊ด๋ฆฌ๊ฐ ์ค์ํ ๊น?
์ผ, ์ฝ๋ ์ง๋ค ๋ณด๋ฉด ์ด๋ฐ ๊ฒฝํ ์์ง ์์? "์ด ์ํ๊ฐ์ด 'pending'์ด์๋ 'PENDING'์ด์๋?" ํ๋ฉด์ ๋ค๋ฅธ ํ์ผ ๋ค์ ๊ฑฐ๋ฆฌ๋ ๊ฑฐ. ๐
ํ๋ก๊ทธ๋๋ฐ์์ ์์ ๊ด๋ฆฌ๋ ์ง์ง ์ค์ํด. ํนํ ํ๋ก์ ํธ๊ฐ ์ปค์ง์๋ก ๋ง์ด์ผ. ์คํ ํ๋๋ก ๋ฒ๊ทธ ์ฐพ๋๋ผ ๋ฐค์ ์๋ ์๊ฑฐ๋ !
2. ์๋์์ฑ: IDE๊ฐ ๊ฐ๋ฅํ ๊ฐ๋ค์ ์๋ ค์ค์ ๊ฐ๋ฐ ์๋ UP โก
3. ์ ์ง๋ณด์: ๊ฐ์ ํ ๊ณณ์์ ๊ด๋ฆฌํ๋๊น ๋ณ๊ฒฝ์ด ์ฌ์
4. ๊ฐ๋ ์ฑ: ๋งค์ง ๋๋ฒ๋ ๋งค์ง ์คํธ๋ง ๋์ ์๋ฏธ์๋ ์ด๋ฆ ์ฌ์ฉ
ํ์ ์คํฌ๋ฆฝํธ๋ ์ด๋ฐ ์์ ๊ด๋ฆฌ๋ฅผ ์ํด ์ฌ๋ฌ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋๋ฐ, ์ค๋์ ๊ทธ ์ค์์๋ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ Enum๊ณผ const assertion์ ๋ํด ๊น์ด ํํค์ณ๋ณผ ๊ฑฐ์ผ! ๐
๐ Enum, ๋๋ ๋๊ตฌ๋?
Enum์ "Enumeration(์ด๊ฑฐ)"์ ์ค์๋ง์ด์ผ. ์ฝ๊ฒ ๋งํ๋ฉด ๊ด๋ จ๋ ์์๋ค์ ์งํฉ์ ์ ์ํ๋ ๊ฑฐ์ง. ํ์ ์คํฌ๋ฆฝํธ์์ ์ ๊ณตํ๋ ๋ช ์ ๋๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ฅ ๊ธฐ๋ฅ ์ค ํ๋์ผ.
๐จ ์ซ์ํ Enum (Numeric Enum)
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ํํ๋ถํฐ ๋ณผ๊น? ์ซ์ํ Enum์ ์๋์ผ๋ก 0๋ถํฐ ์์ํด์ 1์ฉ ์ฆ๊ฐํ๋ ๊ฐ์ ๊ฐ์ ธ.
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
// ์ฌ์ฉ ์์
let playerDirection: Direction = Direction.Up;
console.log(playerDirection); // 0
// ๊ฐ์ผ๋ก ์ ๊ทผ๋ ๊ฐ๋ฅ (์ญ๋ฐฉํฅ ๋งคํ)
console.log(Direction[0]); // "Up"
์ ๊ธฐํ์ง? ๐ฎ Direction.Up์ 0์ด๊ณ , Direction[0]์ "Up"์ ๋ฐํํด. ์ด๊ฑธ ์ญ๋ฐฉํฅ ๋งคํ(Reverse Mapping)์ด๋ผ๊ณ ํด. ์ซ์ํ Enum๋ง ๊ฐ๋ฅํ ํน์ง์ด์ผ!
๋ฌผ๋ก ์ํ๋ ์ซ์๋ถํฐ ์์ํ ์๋ ์์ด:
enum Status {
Pending = 1,
InProgress, // 2
Done // 3
}
// ๋๋ ๊ฐ๊ฐ ๋ค๋ฅธ ๊ฐ ์ง์
enum HttpStatus {
OK = 200,
NotFound = 404,
ServerError = 500
}
๐ค ๋ฌธ์์ด Enum (String Enum)
๊ฐ์ธ์ ์ผ๋ก ์ค๋ฌด์์ ๋ ๋ง์ด ์ฐ๋ ๊ฑด ๋ฌธ์์ด Enum์ด์ผ. ๋๋ฒ๊น ํ ๋ ๊ฐ์ด ๋ช ํํ๊ฒ ๋ณด์ด๊ฑฐ๋ ! ๐
enum UserRole {
Admin = "ADMIN",
Editor = "EDITOR",
Viewer = "VIEWER"
}
function checkPermission(role: UserRole) {
if (role === UserRole.Admin) {
console.log("๋ชจ๋ ๊ถํ ์์! ๐");
} else if (role === UserRole.Editor) {
console.log("ํธ์ง ๊ถํ ์์ โ๏ธ");
} else {
console.log("์ฝ๊ธฐ ์ ์ฉ ๐");
}
}
checkPermission(UserRole.Admin);
๋ฌธ์์ด Enum์ ์ญ๋ฐฉํฅ ๋งคํ์ด ์ ๋ผ. ๋์ ๊ฐ ์์ฒด๊ฐ ์๋ฏธ์๋ ๋ฌธ์์ด์ด๋ผ์ ๋ก๊ทธ ์ฐ์ ๋๋ API ์๋ต ๋ณผ ๋ ํจ์ฌ ์ดํดํ๊ธฐ ์ฌ์! ๐ฏ
๐ญ ์ด์ข Enum (Heterogeneous Enum)
์ซ์์ ๋ฌธ์์ด์ ์์ด ์ธ ์๋ ์์ด. ๊ทผ๋ฐ ์์งํ ๋งํ๋ฉด... ๋ณ๋ก ์ถ์ฒํ์ง ์์. ๐ ํผ๋์ค๋ฌ์ธ ์ ์๊ฑฐ๋ .
enum Mixed {
No = 0,
Yes = "YES"
}
// ๊ฐ๋ฅ์ ํ์ง๋ง... ์ ์ด๋ ๊ฒ ์ฐ์ง? ๐ค
โ๏ธ Enum์ ์ปดํ์ผ ๊ฒฐ๊ณผ์ ํน์ง
์ฌ๊ธฐ์ ์ค์ํ ํฌ์ธํธ! ํ์ ์คํฌ๋ฆฝํธ์ ๋๋ถ๋ถ ๊ธฐ๋ฅ์ ์ปดํ์ผ ํ ์ฌ๋ผ์ง๋๋ฐ, Enum์ ์ค์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ๋ณํ๋ผ. ์ด๊ฒ ์ฅ์ ์ด์ ๋จ์ ์ด ๋ ์ ์์ด. ๐คทโโ๏ธ
๐ฆ ์ซ์ํ Enum์ ์ปดํ์ผ ๊ฒฐ๊ณผ
enum Direction {
Up,
Down,
Left,
Right
}
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 0] = "Up";
Direction[Direction["Down"] = 1] = "Down";
Direction[Direction["Left"] = 2] = "Left";
Direction[Direction["Right"] = 3] = "Right";
})(Direction || (Direction = {}));
์... ๋ณต์กํด ๋ณด์ด์ง? ๐ต ์ด๊ฒ ๋ฐ๋ก ์ญ๋ฐฉํฅ ๋งคํ์ ๊ตฌํํ๋ ๋ฐฉ์์ด์ผ. Direction["Up"] = 0์ ํ ๋นํ๋ฉด์ ๋์์ Direction[0] = "Up"๋ ์ค์ ํ๋ ๊ฑฐ์ง.
์ ์ฝ๋๊ฐ ๋ง๋๋ Direction ๊ฐ์ฒด๋ ์ด๋ ๊ฒ ์๊ฒผ์ด:
{
0: "Up",
1: "Down",
2: "Left",
3: "Right",
Up: 0,
Down: 1,
Left: 2,
Right: 3
}
์๋ฐฉํฅ์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅํ ๊ตฌ์กฐ์ผ! ๐
๐ฏ const Enum - ์ต์ ํ์ ๋น๋ฐ
"Enum์ด ์ฝ๋๋ฅผ ์์ฑํ๋ค๊ณ ? ๋ฒ๋ค ํฌ๊ธฐ๊ฐ ๊ฑฑ์ ๋๋๋ฐ..." ํ๋ ์น๊ตฌ๋ค์ ์ํ ํด๊ฒฐ์ฑ ! const enum์ ์ฐ๋ฉด ์ปดํ์ผ ์ ์ธ๋ผ์ธ์ผ๋ก ์นํ๋ผ์ ์ถ๊ฐ ์ฝ๋๊ฐ ์์ฑ๋์ง ์์. ๐
const enum Direction {
Up,
Down,
Left,
Right
}
let dir = Direction.Up;
// ์ปดํ์ผ ๊ฒฐ๊ณผ
let dir = 0; // ๊ทธ๋ฅ ๊ฐ์ผ๋ก ์นํ!
์์ ๊น๋ํ์ง? ๐ ํ์ง๋ง ์ฃผ์ํ ์ ์ด ์์ด:
์ญ๋ฐฉํฅ ๋งคํ ๋ถ๊ฐ ์ธ๋ถ ๋ชจ๋์์ ์ฌ์ฉ ์ ํ isolatedModules ์ต์ ๊ณผ ์ถฉ๋
ํนํ Babel์ ์ฌ์ฉํ๋ ํ๋ก์ ํธ์์๋ const enum์ด ์ ๋๋ก ๋์ํ์ง ์์ ์ ์์ด. ์ด๋ด ๋ ์ผ๋ฐ enum์ด๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๊ณ ๋ คํด์ผ ํด!
๐ช Enum์ ์ค์ ํ์ฉ ํจํด
์ด๋ก ์ ์ด์ ์ถฉ๋ถํด! ์ค์ ๋ก ์ด๋ป๊ฒ ์ฐ๋์ง ๋ณด์๊ตฌ. ๐ช
๐ฎ ๊ฒ์ ์ํ ๊ด๋ฆฌ
enum GameState {
Menu = "MENU",
Playing = "PLAYING",
Paused = "PAUSED",
GameOver = "GAME_OVER"
}
class Game {
private state: GameState = GameState.Menu;
start() {
if (this.state === GameState.Menu) {
this.state = GameState.Playing;
console.log("๊ฒ์ ์์! ๐ฎ");
}
}
pause() {
if (this.state === GameState.Playing) {
this.state = GameState.Paused;
console.log("์ผ์์ ์ง โธ๏ธ");
}
}
resume() {
if (this.state === GameState.Paused) {
this.state = GameState.Playing;
console.log("์ฌ๊ฐ! โถ๏ธ");
}
}
}
๐ API ์๋ต ์ํ ์ฝ๋
enum ApiStatus {
Success = 200,
Created = 201,
BadRequest = 400,
Unauthorized = 401,
NotFound = 404,
ServerError = 500
}
async function fetchData(url: string) {
const response = await fetch(url);
switch (response.status) {
case ApiStatus.Success:
return await response.json();
case ApiStatus.NotFound:
throw new Error("๋ฆฌ์์ค๋ฅผ ์ฐพ์ ์ ์์ด์ ๐ข");
case ApiStatus.ServerError:
throw new Error("์๋ฒ ์ค๋ฅ ๋ฐ์! ๐ฅ");
default:
throw new Error(`์์์น ๋ชปํ ์ํ: ${response.status}`);
}
}
๐จ ํ ๋ง ๊ด๋ฆฌ
enum Theme {
Light = "light",
Dark = "dark",
Auto = "auto"
}
enum ColorScheme {
Primary = "#6c5ce7",
Secondary = "#74b9ff",
Success = "#00b894",
Warning = "#fdcb6e",
Danger = "#d63031"
}
interface AppConfig {
theme: Theme;
primaryColor: ColorScheme;
}
const config: AppConfig = {
theme: Theme.Dark,
primaryColor: ColorScheme.Primary
};
์ด๋ ๊ฒ Enum์ ์ฐ๋ฉด IDE์์ ์๋์์ฑ๋ ๋๊ณ , ์คํ ๊ฑฑ์ ๋ ์์ด์ ๊ฐ๋ฐ์ด ์ ๋ง ํธํด์ ธ! โจ
๐ฅ const assertion - ํ๋์ ์ธ ๋์
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก const assertion์ ๋ํด ์์๋ณผ ์๊ฐ์ด์ผ! ํ์ ์คํฌ๋ฆฝํธ 3.4 ๋ฒ์ ๋ถํฐ ๋์ ๋ ์ด ๊ธฐ๋ฅ์ Enum์ ๋์์ผ๋ก ๋ง์ด ์ฌ์ฉ๋๊ณ ์์ด. ๐ฏ
๐คทโโ๏ธ const assertion์ด ๋ญ๋ฐ?
const assertion์ as const๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํด์ ๊ฐ์ ๋ฆฌํฐ๋ด ํ์
์ผ๋ก ์ขํ๊ณ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ๋ง๋๋ ๊ธฐ๋ฅ์ด์ผ. ๋ง์ด ์ด๋ ต์ง? ์์ ๋ฅผ ๋ณด๋ฉด ๋ฐ๋ก ์ดํด๋ ๊ฑฐ์ผ! ๐
// ์ผ๋ฐ ๊ฐ์ฒด
const colors1 = {
red: "#ff0000",
blue: "#0000ff"
};
// ํ์
: { red: string; blue: string; }
// const assertion ์ฌ์ฉ
const colors2 = {
red: "#ff0000",
blue: "#0000ff"
} as const;
// ํ์
: { readonly red: "#ff0000"; readonly blue: "#0000ff"; }
// ์ฐจ์ด์ ํ์ธ
colors1.red = "#ff1111"; // โ
๊ฐ๋ฅ
colors2.red = "#ff1111"; // โ ์๋ฌ! ์ฝ๊ธฐ ์ ์ฉ
๋ณด์ด์ง? as const๋ฅผ ๋ถ์ด๋ฉด ๊ฐ์ด ์ ํํ ๋ฆฌํฐ๋ด ํ์
์ผ๋ก ์ถ๋ก ๋๊ณ , ์์ ๋ ๋ถ๊ฐ๋ฅํด์ ธ! ๐
๐ ๋ฐฐ์ด์์์ const assertion
// ์ผ๋ฐ ๋ฐฐ์ด
const fruits1 = ["apple", "banana", "orange"];
// ํ์
: string[]
// const assertion ์ฌ์ฉ
const fruits2 = ["apple", "banana", "orange"] as const;
// ํ์
: readonly ["apple", "banana", "orange"]
// ํํ์ฒ๋ผ ๋์!
const first: "apple" = fruits2[0]; // โ
const second: "banana" = fruits2[1]; // โ
fruits2.push("grape"); // โ ์๋ฌ! ์ฝ๊ธฐ ์ ์ฉ ๋ฐฐ์ด
๐ฏ Enum ๋์ const assertion ์ฌ์ฉํ๊ธฐ
์ด์ ํต์ฌ์ด์ผ! const assertion์ผ๋ก Enum๊ณผ ๋น์ทํ ํจ๊ณผ๋ฅผ ๋ผ ์ ์์ด. ๐
enum UserRole {
Admin = "ADMIN",
Editor = "EDITOR",
Viewer = "VIEWER"
}
function checkRole(role: UserRole) {
// ...
}
checkRole(UserRole.Admin);
const UserRole = {
Admin: "ADMIN",
Editor: "EDITOR",
Viewer: "VIEWER"
} as const;
type UserRole = typeof UserRole[keyof typeof UserRole];
function checkRole(role: UserRole) {
// ...
}
checkRole(UserRole.Admin);
์... const assertion ๋ฐฉ์์ด ์ข ๋ ๋ณต์กํด ๋ณด์ด์ง? ๐ ํ์ง๋ง ์ฅ์ ์ด ์์ด!
2. ํธ๋ฆฌ ์์ดํน: ์ฌ์ฉํ์ง ์๋ ๊ฐ์ ๋ฒ๋ค์์ ์ ๊ฑฐ ๊ฐ๋ฅ
3. ์ ์ฐ์ฑ: ์ผ๋ฐ ๊ฐ์ฒด๋ผ์ ๋ค์ํ ๋ฐฉ์์ผ๋ก ํ์ฉ ๊ฐ๋ฅ
4. ํธํ์ฑ: Babel, esbuild ๋ฑ ๋ชจ๋ ํธ๋์คํ์ผ๋ฌ์ ํธํ
๐ ๏ธ ํ์ ์ถ์ถ ํจํด
const assertion์ ์ฌ์ฉํ ๋ ํ์ ์ ์ถ์ถํ๋ ํจํด์ด ์ค์ํด. ์ฌ๋ฌ ๋ฐฉ๋ฒ์ด ์๋๋ฐ ํ๋์ฉ ๋ณผ๊น? ๐
// ๋ฐฉ๋ฒ 1: typeof์ keyof ์กฐํฉ
const Status = {
Pending: "pending",
InProgress: "in_progress",
Done: "done"
} as const;
type Status = typeof Status[keyof typeof Status];
// "pending" | "in_progress" | "done"
// ๋ฐฉ๋ฒ 2: ๊ฐ ํ์
์ง์ ์ถ์ถ
type StatusValue = typeof Status.Pending | typeof Status.InProgress | typeof Status.Done;
// ๋ฐฉ๋ฒ 3: ์ ํธ๋ฆฌํฐ ํ์
๋ง๋ค๊ธฐ
type ValueOf<T> = T[keyof T];
type Status2 = ValueOf<typeof Status>;
// ์ฌ์ฉ ์์
function updateStatus(status: Status) {
console.log(`์ํ ๋ณ๊ฒฝ: ${status}`);
}
updateStatus(Status.Done); // โ
updateStatus("done"); // โ
๋ฆฌํฐ๋ด ๊ฐ๋ ๊ฐ๋ฅ
updateStatus("invalid"); // โ ํ์
์๋ฌ!
typeof Status[keyof typeof Status] ์ด ๋ถ๋ถ์ด ์ข ํท๊ฐ๋ฆด ์ ์๋๋ฐ, ์ฐจ๊ทผ์ฐจ๊ทผ ํ์ด๋ณด์:
1.
typeof Status โ Status ๊ฐ์ฒด์ ํ์
์ ๊ฐ์ ธ์2.
keyof typeof Status โ "Pending" | "InProgress" | "Done" (ํค๋ค์ ์ ๋์จ)3.
typeof Status[keyof typeof Status] โ ๊ฐ ํค์ ๊ฐ ํ์
๋ค์ ์ ๋์จ๊ฒฐ๊ณผ: "pending" | "in_progress" | "done" ๐
๐จ ์ค์ ํจํด: const assertion ํ์ฉ๋ฒ
์ด์ const assertion์ ์ค์ ๋ก ์ด๋ป๊ฒ ํ์ฉํ๋์ง ๋ค์ํ ํจํด์ ์ดํด๋ณด์! ๐
๐ฏ ํจํด 1: ์ค์ ๊ฐ์ฒด ๊ด๋ฆฌ
// API ์๋ํฌ์ธํธ ๊ด๋ฆฌ
const API_ENDPOINTS = {
users: "/api/users",
posts: "/api/posts",
comments: "/api/comments",
auth: {
login: "/api/auth/login",
logout: "/api/auth/logout",
refresh: "/api/auth/refresh"
}
} as const;
type ApiEndpoint = typeof API_ENDPOINTS;
// ํ์
์์ ํ๊ฒ ์ฌ์ฉ
async function fetchData(endpoint: string) {
const response = await fetch(endpoint);
return response.json();
}
// ์๋์์ฑ ์ง์!
fetchData(API_ENDPOINTS.users);
fetchData(API_ENDPOINTS.auth.login);
๐ฎ ํจํด 2: ์ก์ ํ์ ๊ด๋ฆฌ (Redux ์คํ์ผ)
// ์ก์
ํ์
์ ์
const ActionTypes = {
ADD_TODO: "ADD_TODO",
REMOVE_TODO: "REMOVE_TODO",
TOGGLE_TODO: "TOGGLE_TODO",
SET_FILTER: "SET_FILTER"
} as const;
type ActionType = typeof ActionTypes[keyof typeof ActionTypes];
// ์ก์
์์ฑ์
interface AddTodoAction {
type: typeof ActionTypes.ADD_TODO;
payload: { text: string };
}
interface RemoveTodoAction {
type: typeof ActionTypes.REMOVE_TODO;
payload: { id: number };
}
type TodoAction = AddTodoAction | RemoveTodoAction;
// ๋ฆฌ๋์
function todoReducer(state: any[], action: TodoAction) {
switch (action.type) {
case ActionTypes.ADD_TODO:
// action.payload.text ์๋์์ฑ ๋จ! โจ
return [...state, { text: action.payload.text }];
case ActionTypes.REMOVE_TODO:
// action.payload.id ์๋์์ฑ ๋จ! โจ
return state.filter(todo => todo.id !== action.payload.id);
default:
return state;
}
}
๐จ ํจํด 3: ํ ๋ง ์์คํ
const THEME = {
colors: {
primary: "#6c5ce7",
secondary: "#74b9ff",
success: "#00b894",
warning: "#fdcb6e",
danger: "#d63031",
dark: "#2d3436",
light: "#dfe6e9"
},
spacing: {
xs: "4px",
sm: "8px",
md: "16px",
lg: "24px",
xl: "32px"
},
breakpoints: {
mobile: "320px",
tablet: "768px",
desktop: "1024px",
wide: "1440px"
}
} as const;
type Theme = typeof THEME;
type ThemeColor = typeof THEME.colors[keyof typeof THEME.colors];
type ThemeSpacing = typeof THEME.spacing[keyof typeof THEME.spacing];
// ์ฌ์ฉ ์์
function Button({ color }: { color: ThemeColor }) {
return `<button style="background: ${color}">Click</button>`;
}
Button({ color: THEME.colors.primary }); // โ
Button({ color: "#123456" }); // โ ํ์
์๋ฌ!
๐ ํจํด 4: ๋ค๊ตญ์ด ํค ๊ด๋ฆฌ
const TRANSLATION_KEYS = {
common: {
save: "common.save",
cancel: "common.cancel",
delete: "common.delete",
confirm: "common.confirm"
},
auth: {
login: "auth.login",
logout: "auth.logout",
signUp: "auth.signUp",
forgotPassword: "auth.forgotPassword"
},
errors: {
required: "errors.required",
invalidEmail: "errors.invalidEmail",
networkError: "errors.networkError"
}
} as const;
type TranslationKey = typeof TRANSLATION_KEYS[keyof typeof TRANSLATION_KEYS][string];
// ๋ฒ์ญ ํจ์
function t(key: TranslationKey): string {
// ์ค์ ๋ฒ์ญ ๋ก์ง
return key;
}
// ํ์
์์ ํ๊ฒ ์ฌ์ฉ
console.log(t(TRANSLATION_KEYS.common.save)); // โ
console.log(t(TRANSLATION_KEYS.auth.login)); // โ
console.log(t("random.key")); // โ ํ์
์๋ฌ!
์ด๋ฐ ํจํด๋ค์ ๋ณด๋ฉด ์๊ฒ ์ง๋ง, const assertion์ ์ ๋ง ๋ค์ํ๊ฒ ํ์ฉํ ์ ์์ด! ํนํ ์ค์ ์ด๋ ์์๊ฐ ๋ง์ ํ๋ก์ ํธ์์ ๋น์ ๋ฐํด. โจ
โ๏ธ ์ธ์ ๋ญ ์จ์ผ ํ ๊น?
์, ์ด์ ๊ฐ์ฅ ์ค์ํ ์ง๋ฌธ! "Enum์ ์ธ๊น, const assertion์ ์ธ๊น?" ๐ค
์ ๋ต์... ์ํฉ์ ๋ฐ๋ผ ๋ค๋ฅด๋ค! (์์ํ์ง? ใ
ใ
) ๊ฐ๊ฐ์ ์ฅ๋จ์ ์ ์ ๋ฆฌํด๋ณผ๊ฒ.
โ Enum์ ์ฌ์ฉํ๋ฉด ์ข์ ๊ฒฝ์ฐ
๋นํธ ํ๋๊ทธ๋ ์์๊ฐ ์ค์ํ ๊ฒฝ์ฐ ์ซ์ํ Enum์ด ํธํด.
enum Permission {
None = 0,
Read = 1 << 0, // 1
Write = 1 << 1, // 2
Execute = 1 << 2 // 4
}
const userPermission = Permission.Read | Permission.Write; // 3
2. ์ญ๋ฐฉํฅ ๋งคํ์ด ํ์ํ ๋์ซ์ ๊ฐ์ผ๋ก ์ด๋ฆ์ ์ฐพ์์ผ ํ๋ ๊ฒฝ์ฐ.
enum Status {
Pending,
Active,
Inactive
}
console.log(Status[1]); // "Active"
3. ๋ช
ํํ ํ์
๊ตฌ๋ถ์ด ํ์ํ ๋Enum์ ๊ณ ์ ํ ํ์ ์ ์์ฑํ๋ฏ๋ก ๋ค๋ฅธ ๋ฌธ์์ด๊ณผ ๊ตฌ๋ถ๋ผ.
โ const assertion์ ์ฌ์ฉํ๋ฉด ์ข์ ๊ฒฝ์ฐ
ํ๋ก ํธ์๋ ํ๋ก์ ํธ์์ ๋ฒ๋ค ํฌ๊ธฐ ์ต์ ํ๊ฐ ํ์ํ ๊ฒฝ์ฐ. 2. ํธ๋ฆฌ ์์ดํน์ด ํ์ํ ๋
์ฌ์ฉํ์ง ์๋ ๊ฐ๋ค์ ๋ฒ๋ค์์ ์ ๊ฑฐํ๊ณ ์ถ์ ๋. 3. Babel์ด๋ esbuild๋ฅผ ์ฌ์ฉํ ๋
const enum์ด ์ ๋๋ก ๋์ํ์ง ์๋ ํ๊ฒฝ. 4. ๊ฐ์ฒด์ ์ ์ฐ์ฑ์ด ํ์ํ ๋
์ผ๋ฐ ๊ฐ์ฒด์ด๋ฏ๋ก ๋ค์ํ ๋ฐฉ์์ผ๋ก ์กฐ์ ๊ฐ๋ฅ.
const STATUS = {
pending: "pending",
active: "active"
} as const;
// ๊ฐ์ฒด ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
const allStatuses = Object.values(STATUS);
const statusKeys = Object.keys(STATUS);
๐ฏ ์ค์ฉ์ ์ธ ์ ํ ๊ฐ์ด๋
๋ช ํํ ํ์ ๊ตฌ๋ถ๊ณผ ์์ ์ฑ์ด ์ค์. ๋ฒ๋ค ํฌ๊ธฐ๋ณด๋ค ์ ์ง๋ณด์์ฑ ์ฐ์ .
๋ฒ๋ค ํฌ๊ธฐ ์ต์ ํ๊ฐ ์ค์. ํ๋์ ์ธ ๋น๋ ๋๊ตฌ์ ํธํ์ฑ ์ข์.
์ซ์ ๊ฐ๊ณผ ๋นํธ ์ฐ์ฐ์ด ์์ฃผ ํ์. ์ํ ๊ด๋ฆฌ๊ฐ ๋ณต์กํจ.
๋น ๋ฅธ ๊ฐ๋ฐ๊ณผ ์ ์ฐ์ฑ์ด ์ค์. ์ต์ ๊ธฐ์ ์คํ ์ฌ์ฉ.
๐ง ๊ณ ๊ธ ํ ํฌ๋๊ณผ ํ
์ฌ๊ธฐ์๋ถํฐ๋ ์ข ๋ ๊ณ ๊ธ ๋ด์ฉ์ด์ผ! ์ค๋ฌด์์ ์ ์ฉํ ํ ํฌ๋๋ค์ ๊ณต์ ํ ๊ฒ. ๐
๐ก ์ ํธ๋ฆฌํฐ ํ์ ๋ง๋ค๊ธฐ
// ๊ฐ์ฒด์ ๊ฐ ํ์
์ถ์ถ
type ValueOf<T> = T[keyof T];
// ์ค์ฒฉ ๊ฐ์ฒด์ ๊ฐ ํ์
์ถ์ถ
type DeepValueOf<T> = T extends object
? ValueOf<{ [K in keyof T]: DeepValueOf<T[K]> }>
: T;
const CONFIG = {
api: {
timeout: 5000,
retries: 3
},
ui: {
theme: "dark",
language: "ko"
}
} as const;
type ConfigValue = DeepValueOf<typeof CONFIG>;
// 5000 | 3 | "dark" | "ko"
// Enum ๊ฐ์ ๋ฐฐ์ด๋ก ๋ณํ
function enumToArray<T extends Record<string, string | number>>(
enumObj: T
): Array<T[keyof T]> {
return Object.values(enumObj);
}
enum Color {
Red = "red",
Blue = "blue",
Green = "green"
}
const colors = enumToArray(Color);
// ["red", "blue", "green"]
๐จ ํ์ ๊ฐ๋ ํ์ฉ
const STATUS = {
pending: "pending",
active: "active",
inactive: "inactive"
} as const;
type Status = typeof STATUS[keyof typeof STATUS];
// ํ์
๊ฐ๋ ํจ์
function isStatus(value: unknown): value is Status {
return Object.values(STATUS).includes(value as Status);
}
// ์ฌ์ฉ ์์
function processStatus(input: unknown) {
if (isStatus(input)) {
// ์ฌ๊ธฐ์ input์ Status ํ์
์ผ๋ก ์ขํ์ง!
console.log(`์ ํจํ ์ํ: ${input}`);
} else {
console.log("์๋ชป๋ ์ํ๊ฐ");
}
}
processStatus("pending"); // โ
processStatus("invalid"); // โ
๐ Enum๊ณผ const assertion ํผํฉ ์ฌ์ฉ
// Enum์ผ๋ก ๊ธฐ๋ณธ ๊ตฌ์กฐ ์ ์
enum UserRoleEnum {
Admin = "ADMIN",
Editor = "EDITOR",
Viewer = "VIEWER"
}
// const assertion์ผ๋ก ์ถ๊ฐ ๋ฉํ๋ฐ์ดํฐ
const USER_ROLE_META = {
[UserRoleEnum.Admin]: {
label: "๊ด๋ฆฌ์",
permissions: ["read", "write", "delete"],
level: 3
},
[UserRoleEnum.Editor]: {
label: "ํธ์ง์",
permissions: ["read", "write"],
level: 2
},
[UserRoleEnum.Viewer]: {
label: "๋ทฐ์ด",
permissions: ["read"],
level: 1
}
} as const;
type RoleMeta = typeof USER_ROLE_META;
function getRoleInfo(role: UserRoleEnum) {
const meta = USER_ROLE_META[role];
return `${meta.label} (๋ ๋ฒจ ${meta.level})`;
}
console.log(getRoleInfo(UserRoleEnum.Admin));
// "๊ด๋ฆฌ์ (๋ ๋ฒจ 3)"
๐ฏ ์ ๋ค๋ฆญ๊ณผ ํจ๊ป ์ฌ์ฉํ๊ธฐ
// ์ ๋ค๋ฆญ ์์ ๊ฐ์ฒด
function createEnum<T extends Record<string, string>>(obj: T) {
return Object.freeze(obj) as { readonly [K in keyof T]: T[K] };
}
const HttpMethod = createEnum({
GET: "GET",
POST: "POST",
PUT: "PUT",
DELETE: "DELETE"
});
type HttpMethod = typeof HttpMethod[keyof typeof HttpMethod];
// ์ ๋ค๋ฆญ ํจ์์ ์กฐํฉ
function request<M extends HttpMethod>(
url: string,
method: M,
data?: M extends "GET" ? never : unknown
) {
console.log(`${method} ${url}`);
if (data) {
console.log("Data:", data);
}
}
request("/api/users", HttpMethod.GET); // โ
request("/api/users", HttpMethod.GET, { name: "John" }); // โ ํ์
์๋ฌ!
request("/api/users", HttpMethod.POST, { name: "John" }); // โ
์ด๋ฐ ๊ณ ๊ธ ํ ํฌ๋๋ค์ ํ์ฉํ๋ฉด ํ์ ์์ ์ฑ์ ๊ทน๋ํํ๋ฉด์๋ ์ ์ฐํ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ด! ๐ช
๐ ํํ ์ค์์ ํด๊ฒฐ๋ฒ
์ค๋ฌด์์ ์์ฃผ ๋ง์ฃผ์น๋ ์ค์๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด๋ดค์ด. ๋๋ ๋ค ๊ฒช์ด๋ณธ ๊ฒ๋ค์ด์ผ! ๐
โ ์ค์ 1: const assertion ์์น ์ฐฉ๊ฐ
// ์๋ชป๋ ์
const colors = {
red: "#ff0000" as const,
blue: "#0000ff" as const
};
// ํ์
: { red: "#ff0000"; blue: "#0000ff"; }
// ํ์ง๋ง ๊ฐ์ฒด ์์ฒด๋ ์์ ๊ฐ๋ฅ!
colors.red = "#ff1111"; // โ
๊ฐ๋ฅ (์๋ํ์ง ์์!)
// ์ฌ๋ฐ๋ฅธ ์
const colors = {
red: "#ff0000",
blue: "#0000ff"
} as const;
// ํ์
: { readonly red: "#ff0000"; readonly blue: "#0000ff"; }
colors.red = "#ff1111"; // โ ์๋ฌ! (์๋ํ ๋๋ก)
as const๋ ํญ์ ๊ฐ์ฒด๋ ๋ฐฐ์ด ์ ์ฒด์ ์ ์ฉํด์ผ ํด! ๊ฐ๋ณ ์์ฑ์ ์ ์ฉํ๋ฉด ์ฝ๊ธฐ ์ ์ฉ์ด ์ ๋ผ.
โ ์ค์ 2: Enum ๊ฐ ์ง์ ๋น๊ต
enum Status {
Active = "ACTIVE",
Inactive = "INACTIVE"
}
// API์์ ๋ฐ์ ๋ฐ์ดํฐ
const apiResponse = "ACTIVE";
// ์๋ชป๋ ๋น๊ต
if (apiResponse === Status.Active) {
// ์ด๊ฑด ๋์ํ์ง๋ง...
}
// ๋ ์์ ํ ๋ฐฉ๋ฒ
function isValidStatus(value: string): value is Status {
return Object.values(Status).includes(value as Status);
}
if (isValidStatus(apiResponse)) {
// ํ์
๊ฐ๋๋ก ์์ ํ๊ฒ ์ฒดํฌ!
const status: Status = apiResponse;
}
โ ์ค์ 3: const enum๊ณผ ์ผ๋ฐ ๊ฐ์ฒด ํผ๋
// const enum์ ์ปดํ์ผ ์ ์ธ๋ผ์ธ๋จ
const enum Direction {
Up,
Down
}
// ๋ฐํ์์ ๊ฐ์ฒด๋ก ์ ๊ทผ ๋ถ๊ฐ!
const directions = Object.values(Direction); // โ ์๋ฌ!
// ํด๊ฒฐ: ์ผ๋ฐ enum ์ฌ์ฉ
enum Direction2 {
Up,
Down
}
const directions2 = Object.values(Direction2); // โ
๊ฐ๋ฅ
โ ์ค์ 4: ํ์ ์ถ์ถ ์ค์
const CONFIG = {
timeout: 5000,
retries: 3
} as const;
// ์๋ชป๋ ํ์
์ถ์ถ
type ConfigKey = keyof typeof CONFIG; // "timeout" | "retries" (ํค๋ง!)
// ์ฌ๋ฐ๋ฅธ ํ์
์ถ์ถ
type ConfigValue = typeof CONFIG[keyof typeof CONFIG]; // 5000 | 3 (๊ฐ!)
// ๋ ๋ช
ํํ ๋ฐฉ๋ฒ
type TimeoutValue = typeof CONFIG.timeout; // 5000
type RetriesValue = typeof CONFIG.retries; // 3
ํ์ ์ด ์์๊ณผ ๋ค๋ฅด๊ฒ ์ถ๋ก ๋ ๋๋ VSCode์์ ๋ณ์์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ค๋ณด๊ฑฐ๋, ๋ค์ ์ ํธ๋ฆฌํฐ ํ์ ์ ์ฌ์ฉํด๋ด:
type Debug<T> = { [K in keyof T]: T[K] };
type MyType = Debug<typeof CONFIG>;
// ํ์
๊ตฌ์กฐ๊ฐ ๋ช
ํํ๊ฒ ๋ณด์!
๐ ์ค์ ํ๋ก์ ํธ ์์
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ข ํฉํด์ ์ค์ ํ๋ก์ ํธ์์ ์ฌ์ฉํ ๋งํ ์์ ๋ฅผ ๋ง๋ค์ด๋ณผ๊ฒ! ๐
๐ Todo ์ฑ ์ํ ๊ด๋ฆฌ
// ์ํ ์ ์
const TODO_STATUS = {
pending: "pending",
inProgress: "in_progress",
completed: "completed",
archived: "archived"
} as const;
type TodoStatus = typeof TODO_STATUS[keyof typeof TODO_STATUS];
// ์ฐ์ ์์ ์ ์
enum Priority {
Low = 1,
Medium = 2,
High = 3,
Urgent = 4
}
// Todo ์ธํฐํ์ด์ค
interface Todo {
id: string;
title: string;
status: TodoStatus;
priority: Priority;
createdAt: Date;
updatedAt: Date;
}
// ์ํ ์ ํ ๊ท์น
const STATUS_TRANSITIONS = {
[TODO_STATUS.pending]: [TODO_STATUS.inProgress, TODO_STATUS.archived],
[TODO_STATUS.inProgress]: [TODO_STATUS.completed, TODO_STATUS.pending],
[TODO_STATUS.completed]: [TODO_STATUS.archived],
[TODO_STATUS.archived]: []
} as const;
// ์ํ ์ ํ ๊ฐ๋ฅ ์ฌ๋ถ ์ฒดํฌ
function canTransition(from: TodoStatus, to: TodoStatus): boolean {
const allowedTransitions = STATUS_TRANSITIONS[from];
return allowedTransitions.includes(to as any);
}
// Todo ๊ด๋ฆฌ ํด๋์ค
class TodoManager {
private todos: Map<string, Todo> = new Map();
updateStatus(id: string, newStatus: TodoStatus): boolean {
const todo = this.todos.get(id);
if (!todo) return false;
if (!canTransition(todo.status, newStatus)) {
console.log(`โ ${todo.status}์์ ${newStatus}๋ก ์ ํ ๋ถ๊ฐ`);
return false;
}
todo.status = newStatus;
todo.updatedAt = new Date();
console.log(`โ
์ํ ๋ณ๊ฒฝ: ${newStatus}`);
return true;
}
getByPriority(priority: Priority): Todo[] {
return Array.from(this.todos.values())
.filter(todo => todo.priority === priority);
}
getByStatus(status: TodoStatus): Todo[] {
return Array.from(this.todos.values())
.filter(todo => todo.status === status);
}
}
๐ฎ ๊ฒ์ ์บ๋ฆญํฐ ์์คํ
// ์บ๋ฆญํฐ ํด๋์ค
enum CharacterClass {
Warrior = "WARRIOR",
Mage = "MAGE",
Archer = "ARCHER",
Healer = "HEALER"
}
// ์คํฌ ํ์
const SKILL_TYPE = {
attack: "attack",
defense: "defense",
heal: "heal",
buff: "buff"
} as const;
type SkillType = typeof SKILL_TYPE[keyof typeof SKILL_TYPE];
// ํด๋์ค๋ณ ๊ธฐ๋ณธ ์คํฏ
const CLASS_BASE_STATS = {
[CharacterClass.Warrior]: {
hp: 150,
mp: 50,
attack: 20,
defense: 15,
speed: 8
},
[CharacterClass.Mage]: {
hp: 80,
mp: 150,
attack: 25,
defense: 5,
speed: 10
},
[CharacterClass.Archer]: {
hp: 100,
mp: 80,
attack: 22,
defense: 8,
speed: 15
},
[CharacterClass.Healer]: {
hp: 90,
mp: 120,
attack: 10,
defense: 10,
speed: 12
}
} as const;
type ClassStats = typeof CLASS_BASE_STATS[CharacterClass];
// ์บ๋ฆญํฐ ์ธํฐํ์ด์ค
interface Character {
name: string;
class: CharacterClass;
level: number;
stats: ClassStats;
}
// ์บ๋ฆญํฐ ์์ฑ ํฉํ ๋ฆฌ
function createCharacter(
name: string,
characterClass: CharacterClass
): Character {
return {
name,
class: characterClass,
level: 1,
stats: { ...CLASS_BASE_STATS[characterClass] }
};
}
// ์ฌ์ฉ ์์
const warrior = createCharacter("์ฉ์ฌ", CharacterClass.Warrior);
console.log(`${warrior.name}์ HP: ${warrior.stats.hp}`);
// "์ฉ์ฌ์ HP: 150"
๐ API ํด๋ผ์ด์ธํธ
// HTTP ๋ฉ์๋
const HTTP_METHOD = {
GET: "GET",
POST: "POST",
PUT: "PUT",
PATCH: "PATCH",
DELETE: "DELETE"
} as const;
type HttpMethod = typeof HTTP_METHOD[keyof typeof HTTP_METHOD];
// API ์๋ํฌ์ธํธ
const API_ENDPOINT = {
users: "/api/users",
posts: "/api/posts",
comments: "/api/comments"
} as const;
type ApiEndpoint = typeof API_ENDPOINT[keyof typeof API_ENDPOINT];
// ์๋ต ์ํ
enum ResponseStatus {
Success = 200,
Created = 201,
BadRequest = 400,
Unauthorized = 401,
NotFound = 404,
ServerError = 500
}
// API ํด๋ผ์ด์ธํธ
class ApiClient {
private baseUrl: string;
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}
async request<T>(
endpoint: ApiEndpoint,
method: HttpMethod,
data?: unknown
): Promise<T> {
const url = `${this.baseUrl}${endpoint}`;
const response = await fetch(url, {
method,
headers: {
"Content-Type": "application/json"
},
body: data ? JSON.stringify(data) : undefined
});
switch (response.status) {
case ResponseStatus.Success:
case ResponseStatus.Created:
return await response.json();
case ResponseStatus.BadRequest:
throw new Error("์๋ชป๋ ์์ฒญ์
๋๋ค");
case ResponseStatus.Unauthorized:
throw new Error("์ธ์ฆ์ด ํ์ํฉ๋๋ค");
case ResponseStatus.NotFound:
throw new Error("๋ฆฌ์์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค");
case ResponseStatus.ServerError:
throw new Error("์๋ฒ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค");
default:
throw new Error(`์์์น ๋ชปํ ์ํ: ${response.status}`);
}
}
// ํธ์ ๋ฉ์๋
get<T>(endpoint: ApiEndpoint) {
return this.request<T>(endpoint, HTTP_METHOD.GET);
}
post<T>(endpoint: ApiEndpoint, data: unknown) {
return this.request<T>(endpoint, HTTP_METHOD.POST, data);
}
put<T>(endpoint: ApiEndpoint, data: unknown) {
return this.request<T>(endpoint, HTTP_METHOD.PUT, data);
}
delete<T>(endpoint: ApiEndpoint) {
return this.request<T>(endpoint, HTTP_METHOD.DELETE);
}
}
// ์ฌ์ฉ ์์
const client = new ApiClient("https://api.example.com");
async function fetchUsers() {
try {
const users = await client.get(API_ENDPOINT.users);
console.log("์ฌ์ฉ์ ๋ชฉ๋ก:", users);
} catch (error) {
console.error("์๋ฌ ๋ฐ์:", error);
}
}
์ด๋ฐ ์์ผ๋ก Enum๊ณผ const assertion์ ์กฐํฉํ๋ฉด ํ์ ์์ ํ๋ฉด์๋ ์ ์ง๋ณด์ํ๊ธฐ ์ฌ์ด ์ฝ๋๋ฅผ ๋ง๋ค ์ ์์ด! ๐
๐ ์ฑ๋ฅ๊ณผ ์ต์ ํ
๋ง์ง๋ง์ผ๋ก ์ฑ๋ฅ ๊ด๋ จ ์ด์ผ๊ธฐ๋ฅผ ํด๋ณผ๊ฒ. ์ค์ ํ๋ก๋์ ํ๊ฒฝ์์ ์ค์ํ ๋ถ๋ถ์ด๊ฑฐ๋ ! โก
๐ ๋ฒ๋ค ํฌ๊ธฐ ๋น๊ต
10๊ฐ์ ์์๋ฅผ ์ ์ํ๊ณ ๊ฐ๊ฐ 5๋ฒ์ฉ ์ฌ์ฉํ์ ๋์ ๋ฒ๋ค ํฌ๊ธฐ ๋น๊ต
// 1. ์ผ๋ฐ Enum (์ฝ 250 bytes)
enum Status {
Pending = "pending",
Active = "active",
// ... 8๊ฐ ๋
}
// 2. const Enum (์ฝ 50 bytes)
const enum Status {
Pending = "pending",
Active = "active",
// ... 8๊ฐ ๋
}
// 3. const assertion (์ฝ 80 bytes)
const STATUS = {
pending: "pending",
active: "active",
// ... 8๊ฐ ๋
} as const;
๊ฒฐ๊ณผ: const enum์ด ๊ฐ์ฅ ์์ง๋ง, const assertion๋ ์ถฉ๋ถํ ์๊ณ ํธํ์ฑ์ด ์ข์! ๐
โก ๋ฐํ์ ์ฑ๋ฅ
// ์ฑ๋ฅ ํ
์คํธ ํจ์
function benchmark(name: string, fn: () => void, iterations: number) {
const start = performance.now();
for (let i = 0; i < iterations; i++) {
fn();
}
const end = performance.now();
console.log(`${name}: ${(end - start).toFixed(2)}ms`);
}
// Enum ์ฌ์ฉ
enum Status1 { Pending, Active, Done }
benchmark("Enum", () => {
const s = Status1.Active;
}, 1000000);
// const assertion ์ฌ์ฉ
const STATUS2 = { pending: 0, active: 1, done: 2 } as const;
benchmark("const assertion", () => {
const s = STATUS2.active;
}, 1000000);
// ๊ฒฐ๊ณผ: ๊ฑฐ์ ์ฐจ์ด ์์! (๋ ๋ค ๋จ์ ๊ฐ์ฒด ์ ๊ทผ)
๋ฐํ์ ์ฑ๋ฅ์ ๊ฑฐ์ ์ฐจ์ด๊ฐ ์์ด. ๋ ๋ค ๊ฒฐ๊ตญ ๊ฐ์ฒด ํ๋กํผํฐ ์ ๊ทผ์ด๋๊น! ์ค์ํ ๊ฑด ๋ฒ๋ค ํฌ๊ธฐ์ ๊ฐ๋ฐ ๊ฒฝํ์ด์ผ. ๐
๐ฏ ์ต์ ํ ํ
const assertion์ ์ฌ์ฉํ๋ฉด ์ฌ์ฉํ์ง ์๋ ๊ฐ๋ค์ด ๋ฒ๋ค์์ ์ ๊ฑฐ๋ ์ ์์ด.
// ์ด ์ค์์ pending๋ง ์ฌ์ฉํ๋ค๋ฉด
const STATUS = {
pending: "pending",
active: "active",
done: "done"
} as const;
// ๋ฒ๋ค์๋ pending๋ง ํฌํจ๋ ์ ์์!
2. ์ฝ๋ ์คํ๋ฆฌํ
ํฐ Enum์ด๋ ์์ ๊ฐ์ฒด๋ ๋ณ๋ ํ์ผ๋ก ๋ถ๋ฆฌํ๊ณ ๋์ import ์ฌ์ฉ.
// constants.ts
export const LARGE_CONFIG = { /* ๋ง์ ์ค์ */ } as const;
// main.ts
const config = await import("./constants");
// ํ์ํ ๋๋ง ๋ก๋!
3. ํ์
๋ง importํ์ ๋ง ํ์ํ ๊ฒฝ์ฐ type import ์ฌ์ฉ.
import type { Status } from "./types";
// ๋ฐํ์ ์ฝ๋์ ํฌํจ๋์ง ์์!
๐ฏ ์ฌ๋ฅ๋ท์์ ๋ฐฐ์ฐ๋ ํ์ ์คํฌ๋ฆฝํธ
ํ์
์คํฌ๋ฆฝํธ๋ฅผ ๋ ๊น์ด ๋ฐฐ์ฐ๊ณ ์ถ๋ค๋ฉด ์ฌ๋ฅ๋ท์ ํ์ฉํด๋ณด๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ! ๐
์ฌ๋ฅ๋ท(https://www.jaenung.net)์ '์ง์์ธ์ ์ฒ'์๋ ์ด๋ฐ ํ๋ก๊ทธ๋๋ฐ ์ง์๋ค์ด ๊ฐ๋ํ๊ณ , ์ค๋ฌด ๊ฒฝํ์ด ํ๋ถํ ๊ฐ๋ฐ์๋ค์๊ฒ ์ง์ ๋ฐฐ์ธ ์๋ ์์ด. ํนํ ํ์
์คํฌ๋ฆฝํธ ๊ฐ์ ๊ธฐ์ ์ ์ค์ ๊ฒฝํ์ด ์ค์ํ๋ฐ, ์ฌ๋ฅ๋ท์์ ๋ฉํ ๋ฅผ ์ฐพ์ 1:1๋ก ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ๊ฑฐ๋ ํ๋ก์ ํธ ์๋ด์ ๋ฐ์ผ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ฒ ์ฑ์ฅํ ์ ์์ง! ๐ช
2. ํ์ ์๋ฌ๋ฅผ ๋๋ ค์ํ์ง ๋ง: ์๋ฌ ๋ฉ์์ง๊ฐ ์น์ ํ๊ฒ ์๋ ค์ฃผ๋๊น ์คํ๋ ค ๋ฐฐ์ฐ๊ธฐ ์ข์
3. ์ค๋ฌด ์ฝ๋ ์ฐธ๊ณ : ์คํ์์ค ํ๋ก์ ํธ๋ค์ด ์ด๋ป๊ฒ ์์๋ฅผ ๊ด๋ฆฌํ๋์ง ์ดํด๋ด
4. ํ ์ปจ๋ฒค์ ์ ํ๊ธฐ: ํ๋ก์ ํธ๋ง๋ค ์ผ๊ด๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ ๊ฒ ์ค์ํด
๐ฌ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์... ์ฌ๊ธฐ๊น์ง ์ฝ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ๐๐๐
์ฐ๋ฆฌ๊ฐ ์ค๋ ๋ค๋ฃฌ ๋ด์ฉ์ ์ ๋ฆฌํด๋ณผ๊น?
โข ํ์ ์คํฌ๋ฆฝํธ์ ์ ํต์ ์ธ ์์ ๊ด๋ฆฌ ๋ฐฉ๋ฒ
โข ์ซ์ํ, ๋ฌธ์์ดํ, ์ด์ข Enum ์ง์
โข ์ญ๋ฐฉํฅ ๋งคํ ๊ฐ๋ฅ (์ซ์ํ๋ง)
โข ์ถ๊ฐ ์ฝ๋ ์์ฑ์ผ๋ก ๋ฒ๋ค ํฌ๊ธฐ ์ฆ๊ฐ
โ const assertion
โข ํ๋์ ์ด๊ณ ๊ฐ๋ฒผ์ด ๋์
โข ์ถ๊ฐ ์ฝ๋ ์์ฑ ์์
โข ํธ๋ฆฌ ์์ดํน ๊ฐ๋ฅ
โข ๋ชจ๋ ๋น๋ ๋๊ตฌ์ ํธํ
โ ์ ํ ๊ธฐ์ค
โข ๋ฒ๋ค ํฌ๊ธฐ ์ค์ โ const assertion
โข ์ซ์ ๊ฐ/์ญ๋ฐฉํฅ ๋งคํ ํ์ โ Enum
โข ๋ช ํํ ํ์ ๊ตฌ๋ถ ํ์ โ Enum
โข ์ ์ฐ์ฑ๊ณผ ํธํ์ฑ ์ค์ โ const assertion
๊ฒฐ๊ตญ ์ ๋ต์ ์์ด. ํ๋ก์ ํธ์ ์๊ตฌ์ฌํญ, ํ์ ์ ํธ๋, ๊ธฐ์ ์คํ์ ๋ฐ๋ผ ์ ์ ํ ๋ฐฉ๋ฒ์ ์ ํํ๋ฉด ๋ผ. ์ค์ํ ๊ฑด ์ผ๊ด์ฑ์ด์ผ! ๐ฏ
์ฒ์์๋ ์ด๋ ต๊ฒ ๋๊ปด์ง ์ ์์ง๋ง, ๊ณ์ ์ฌ์ฉํ๋ค ๋ณด๋ฉด ์์ฐ์ค๋ฝ๊ฒ ์ต์ํด์ง ๊ฑฐ์ผ. ํ์
์คํฌ๋ฆฝํธ์ ํ์
์์คํ
์ ์ ๋ง ๊ฐ๋ ฅํ๊ณ , ์ ๋๋ก ํ์ฉํ๋ฉด ๋ฒ๊ทธ๋ฅผ ์ฌ์ ์ ๋ง์ด ์ก์ ์ ์์ด. ๐
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ค์ ํ๋ก์ ํธ์ ์ ์ฉํด๋ด! ์์ ๊ฒ๋ถํฐ ์์ํด์ ์ ์ ๋ณต์กํ ํจํด์ผ๋ก ๋ฐ์ ์์ผ ๋๊ฐ๋ฉด ๋ผ.
๊ถ๊ธํ ์ ์ด ์๊ฑฐ๋ ๋ ๊น์ด ๋ฐฐ์ฐ๊ณ ์ถ๋ค๋ฉด ํ์ ์คํฌ๋ฆฝํธ ๊ณต์ ๋ฌธ์๋ ์ปค๋ฎค๋ํฐ๋ฅผ ํ์ฉํ๊ณ , ์ค๋ฌด ๊ฒฝํ์ด ํ์ํ๋ค๋ฉด ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๋ฉํ ๋ฅผ ์ฐพ์๋ณด๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ! ๐ช
ํ๋ณตํ ์ฝ๋ฉ ๋๊ธธ ๋ฐ๋ผ! ๐โจ
ํ์
์์ ํ ์ฝ๋๋ก ๋ฒ๊ทธ ์๋ ์ธ์์ ๋ง๋ค์ด๋ณด์! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.