TypeScript 5.0 decorator 마이그레이션 후기

배경

프로젝트를 TypeScript 5.0으로 업그레이드하면서 decorator 관련 경고가 발생했다. 기존에 NestJS 프로젝트에서 experimentalDecorators: true 옵션을 사용하고 있었는데, TS 5.0부터 표준 decorator 문법이 지원되면서 마이그레이션을 검토하게 되었다.

주요 변경사항

1. 메타데이터 API 차이

기존 experimental decorator는 reflect-metadata를 사용했지만, 표준 decorator는 메타데이터 API가 다르다. NestJS는 아직 experimental decorator에 의존하고 있어서 당장 마이그레이션은 불가능했다.

// experimental (기존)
@Injectable()
export class UserService {
  constructor(private repo: UserRepository) {}
}

// 표준 문법에서는 메타데이터 추론이 달라짐

2. 현실적인 대응

결국 experimentalDecorators를 유지하기로 결정했다. NestJS 공식 문서에서도 당분간 experimental decorator를 사용하도록 안내하고 있었다.

다만 새로 작성하는 유틸리티 decorator는 표준 문법을 고려해 작성했다.

// 표준 문법 호환 방식
function measure(target: any, context: ClassMethodDecoratorContext) {
  const methodName = String(context.name);
  
  return function(this: any, ...args: any[]) {
    const start = performance.now();
    const result = target.apply(this, args);
    console.log(`${methodName}: ${performance.now() - start}ms`);
    return result;
  };
}

결론

TypeScript 5.0의 표준 decorator는 매력적이지만, 프레임워크 생태계가 따라오기까지 시간이 필요하다. 당장은 experimental decorator를 유지하되, 프레임워크 업데이트를 지켜보면서 점진적으로 마이그레이션 계획을 세울 예정이다.