계산된 멤버와 상수 멤버
계산된 멤버
런타임에 값이 계산되는 멤버
복잡한 표현식을 사용할 수 있지만, 다른 멤버의 값에는 영향을 주지 못함
enum FileAccess {
Read = 1 << 1, // 계산된 값 (2)
Write = Read | 1, // 계산된 값 (3)
Complex = "FileAccess".length // 문자열 연산 결과 (11)
}
console.log(FileAccess.Write); // 3
console.log(FileAccess.Complex); // 11
상수 멤버
컴파일 타임에 값이 결정되는 멤버로, 단순하고 빠르다
초기화된 값: 숫자, 문자열, 다른 상수 열거형 참조
암시적 초기화: 숫자 열거형에서 값 생략 시 자동 증가
enum Status {
start, // 0 (암시적 초기화)
Progress = 1, // 초기화된 상수
end = Progress // 다른 상수 참조
}
console.log(Status.start); // 0
console.log(Status.end); // 1
유니온 열거형
열거형 멤버들의 값을 리터럴 타입으로 사용하여 유니온 타입을 정의
열거형의 값이 상수(const) 열거형이어야 함
type Transport =
| { mode: TrafficMode.Car; speed: number; fuel: number }
| { mode: TrafficMode.Bike; speed: number; helmet: boolean }
| { mode: TrafficMode.Bus; speed: number; capacity: number };
function getTransportInfo(transport: Transport): string {
switch (transport.mode) {
case TrafficMode.Car:
return `Car is moving at ${transport.speed} km/h with ${transport.fuel}% fuel.`;
case TrafficMode.Bike:
return `Bike is moving at ${transport.speed} km/h. Helmet worn: ${transport.helmet}`;
case TrafficMode.Bus:
return `Bus is moving at ${transport.speed} km/h with a capacity of ${transport.capacity} people.`;
default:
return "Unknown transport mode.";
}
}
// Example usage
const carInfo = getTransportInfo({ mode: TrafficMode.Car, speed: 120, fuel: 80 });
console.log(carInfo); // "Car is moving at 120 km/h with 80% fuel."
열거형 멤버 타입
특정 열거형의 멤버들만을 허용하는 타입을 의미
enum OrderStatus {
Pending = "PENDING",
Shipped = "SHIPPED",
Delivered = "DELIVERED",
Canceled = "CANCELED",
}
// 열거형 멤버 타입
type ActiveStatus = OrderStatus.Pending | OrderStatus.Shipped;
// 함수 정의
function updateOrderStatus(status: ActiveStatus): void {
if (status === OrderStatus.Pending) {
console.log("Order is still pending.");
} else if (status === OrderStatus.Shipped) {
console.log("Order has been shipped.");
}
}
// 사용 예시
updateOrderStatus(OrderStatus.Pending); // Valid
updateOrderStatus(OrderStatus.Shipped); // Valid
// updateOrderStatus(OrderStatus.Delivered); // Error: 'OrderStatus.Delivered' is not assignable
런타임 열거형
런타임에 존재하는 실제 객체
enum Alphabet {
X, Y, Z
}
function f(obj: { X: number }) {
return obj.X;
}
f(Alphabet);
// E가 X라는 숫자 프로퍼티를 가지고 있기 때문에 동작하는 코드
console.log(f(Alphabet));
컴파일 열거형
상수 열거형(const enum)을 사용해 생성
컴파일러가 열거형을 상수로 직접 인라인 처리하여 성능을 최적화
const enum State {
Active = 1,
Inactive,
Pending,
}
const nowStatus = State.Active;
console.log(nowStatus); // 컴파일 시 1로 대체
//컴파일 결과(JS코드)
// const nowStatus = 1;
// console.log(nowStatus);