TypeScript 5 implements TC39 Stage 3 decorators, replacing the experimental decorator API. Learn vocabulary for the DecoratorContext object, kind property, addInitializer timing, Symbol.metadata for standards-compliant metadata storage, and the breaking differences from experimentalDecorators.
0 / 5 completed
1 / 5
TypeScript 5.0 introduced decorators aligned with the TC39 Stage 3 proposal. How do these differ from the legacy 'experimental' decorators?
TC39 Stage 3 decorators use a new signature: (value, context) where context is a DecoratorContext object containing kind, name, addInitializer, etc. Legacy decorators used (target, propertyKey, descriptor). The two are incompatible and controlled by the experimentalDecorators tsconfig flag.
2 / 5
A developer writes a class decorator that returns a new class. What must the returned class extend to be valid under TC39 decorator semantics?
A class decorator may return a new class, but it must extend the original class to maintain the prototype chain and type compatibility. Returning an unrelated class would break TypeScript's type inference and runtime behavior for instanceof checks.
3 / 5
What is Symbol.metadata used for in the TC39 decorator metadata proposal?
Symbol.metadata is a well-known symbol (TC39 proposal) where decorators store metadata on the class constructor. Decorators can call context.metadata[myKey] = value, and the metadata is accessible at runtime via MyClass[Symbol.metadata]. This replaces the Reflect.metadata pattern from reflect-metadata.
4 / 5
A method decorator's context.addInitializer(fn) is called. When does fn execute?
addInitializer registers a function that runs during class instantiation, after the constructor completes, with this bound to the new instance. For static decorators, it runs once when the class is defined. This enables decorators to perform per-instance setup (e.g., binding methods, registering event listeners).
5 / 5
Which TypeScript compiler option must be enabled to use TC39 Stage 3 decorators (not legacy experimental decorators) in TypeScript 5?
TC39 Stage 3 decorators are the default in TypeScript 5 when experimentalDecorators is absent or set to false. Setting experimentalDecorators: true opts into the legacy behavior. You do not need any additional flag for the new decorators — they are the standard going forward.