TypeScript 5.0 베타 - Decorator 정식 지원

배경

회사 프로젝트에서 NestJS를 사용하면서 Decorator를 계속 써왔지만, tsconfig.json에 experimentalDecorators: true 설정이 항상 필요했다. 실험적 기능이라는 점이 찝찝했는데, TypeScript 5.0 베타에서 드디어 Stage 3 Decorator 표준을 지원한다고 발표했다.

변경 사항

기존 experimentalDecorators와 새로운 표준 Decorator는 문법이 약간 다르다.

// 기존 방식 (experimentalDecorators)
function Logger(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  const original = descriptor.value;
  descriptor.value = function(...args: any[]) {
    console.log(`Called: ${propertyKey}`);
    return original.apply(this, args);
  };
}

// 새로운 표준 방식
function Logger(target: Function, context: ClassMethodDecoratorContext) {
  return function(...args: any[]) {
    console.log(`Called: ${String(context.name)}`);
    return target.apply(this, args);
  };
}

마이그레이션 고려사항

NestJS, TypeORM 등 기존 라이브러리들은 아직 experimentalDecorators 기반이다. 당장 전환은 어렵고, 라이브러리 생태계가 따라오는 걸 지켜봐야 할 것 같다.

그래도 표준화가 진행된다는 점은 긍정적이다. 새로운 프로젝트에서는 표준 Decorator를 고려해볼 만하다.

기타 개선사항

  • const type parameter 추가
  • enum 성능 개선
  • 번들 사이즈 감소

아직 베타라 프로덕션 적용은 좀 더 지켜보고, 정식 릴리즈 후 검토할 예정이다.