Skip to content

TS Decorators

TypeScript

Use decorators for class/property/method metadata and modification.

Official Site
01

Overview

Decorators are functions that modify classes/properties/methods. @expression syntax. Class, method, property, parameter decorators. Used by Angular, NestJS, TypeORM. Stage 3 proposal (legacy: experimentalDecorators).

bash
function log(target,key,desc){const orig=desc.value;desc.value=function(...a){console.log(key);return orig.apply(this,a);};}
class S{@log greet(){return "hi";}}
// Factory
function bound(t,k,d){return d;}
02

Best Practices

Enable experimentalDecorators in tsconfig. Use decorator factories for params. Common in Angular/NestJS/TypeORM. Stage 3 decorators differ from legacy. Use reflect-metadata for type info. Prefer composition when possible.

Ready to try TS Decorators?

Visit the official site for the latest version and full documentation.

Visit TS Decorators