TypeScript Syntax

Decorators — modern method decoration

Modern decorators receive a decorated value and a context describing the declaration. A method decorator can return a compatible replacement or leave the method unchanged. This fixture uses the modern model, not experimentalDecorators. Legacy decorators have different signatures and metadata behavior. Observed output: 'read\n4'. Local verification: TypeScript 5.8.3, strict mode, ES2022, CommonJS, Node.js 22.16.0. Disposable library declarations are enabled for the fixture suite.

Formal Syntax & Structure

typescript syntaxtypescript
class C { @decorator method() {} }

Working Example

Example Snippettypescript
function trace(original: (this: Counter) => number, context: ClassMethodDecoratorContext<Counter, (this: Counter) => number>) {  return function(this: Counter) { console.log(String(context.name)); return original.call(this); };}class Counter { @trace read() { return 4; } }console.log(new Counter().read());
TypeScript Reference