TypeScript 5.0 베타 주요 변경사항 정리
배경
어제 TypeScript 5.0 베타가 발표되었다. 메이저 버전 업데이트인 만큼 몇 가지 주요 변경사항을 검토해봤다.
주요 변경사항
Decorator 표준화
가장 눈에 띄는 변경은 Stage 3 Decorator 지원이다. 기존 experimental decorator와 달리 표준안을 따른다.
function log(target: any, context: ClassMethodDecoratorContext) {
const methodName = String(context.name);
return function(this: any, ...args: any[]) {
console.log(`Calling ${methodName}`);
return target.apply(this, args);
};
}
class API {
@log
fetchData() {
// ...
}
}
기존 프로젝트는 experimentalDecorators 옵션을 유지하면 된다.
const Type Parameters
제네릭에 const 수식어를 붙여 더 정확한 타입 추론이 가능해졌다.
function createConfig<const T>(config: T) {
return config;
}
const config = createConfig({ endpoint: '/api', timeout: 3000 });
// config는 { readonly endpoint: '/api', readonly timeout: 3000 }
as const 없이도 리터럴 타입이 보존된다.
Enum 성능 개선
Enum이 컴파일 시 더 간결한 코드로 변환된다. 번들 사이즈 측면에서 유리하다. 기존에는 IIFE를 생성했지만, 5.0부터는 단순 객체로 변환된다.
기타
--moduleResolution bundler옵션 추가tsconfig.json에서extends다중 상속 지원- 모든 enum에서 union enum 특성 지원
소감
Decorator 표준화는 반가운 소식이다. NestJS, TypeORM 등 데코레이터 의존도가 높은 프로젝트들이 표준 문법으로 마이그레이션할 수 있는 기반이 마련됐다. 당장 프로덕션에 적용하긴 어렵지만, 정식 출시 후 검토해볼 예정이다.