TypeScript 5.0 베타 - satisfies 연산자 실전 적용기
배경
프로젝트에서 설정 객체를 다룰 때 타입 안정성과 자동완성을 동시에 확보하기 어려운 문제가 있었다. as const를 쓰면 타입이 너무 좁아지고, 타입 어노테이션을 쓰면 리터럴 타입이 사라지는 상황이었다.
// 기존 방식의 문제점
const config: Record<string, string> = {
apiUrl: 'https://api.example.com',
timeout: '3000' // 오타 발견 못함
};
satisfies 연산자
TypeScript 5.0 베타의 satisfies 연산자를 적용해봤다.
type Config = {
apiUrl: string;
timeout: number;
retryCount: number;
};
const config = {
apiUrl: 'https://api.example.com',
timeout: 3000,
retryCount: 3
} satisfies Config;
// config.apiUrl은 string이 아닌 정확한 리터럴 타입 유지
// 동시에 Config 타입 체크도 통과
실전 사용 케이스
라우트 설정에 적용했더니 효과가 좋았다.
type Route = {
path: string;
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
};
const routes = {
getUser: { path: '/users/:id', method: 'GET' },
createUser: { path: '/users', method: 'POST' }
} satisfies Record<string, Route>;
// routes.getUser.path는 string이 아닌 '/users/:id' 타입
결론
기존에는 as const와 타입 어노테이션 사이에서 타협해야 했는데, satisfies로 두 마리 토끼를 잡을 수 있게 됐다. 아직 베타 단계지만 정식 릴리즈되면 적극 활용할 예정이다.