Decorators in TypeScript: Code Reusability
I had a class with two methods. Both needed logging. So I copy-pasted console.log calls into each one.
24 Apr 2024 · 2 min read

I had a class with two methods. Both needed logging. So I copy-pasted console.log calls into each one.
class User {
constructor(private name: string, private age: number) {}
greet() {
console.log('start: greet')
console.log(`Hello, my name is ${this.name}.`);
console.log('end: greet')
}
printAge() {
console.log('start: printAge')
console.log(`I am ${this.age} years old`);
console.log('end: printAge')
}
}
const user = new User("Ron", 25);
user.greet();
user.printAge();
Two methods. Fine. Ten methods? That's a maintenance nightmare.
Decorators fix this. A decorator is a function you attach to a method (or class, property, parameter). It wraps behavior around the original without touching the original.
Here's a logger decorator:
function logger(originalMethod: any, _context: any) {
function replacementMethod(this: any, ...args: any[]) {
console.log("start:", originalMethod.name);
const result = originalMethod.call(this, ...args);
console.log("end:", originalMethod.name);
return result;
}
return replacementMethod;
}
Now the class becomes clean:
class User {
constructor(private name: string, private age: number) {}
@logger
greet() {
console.log(`Hello, my name is ${this.name}.`);
}
@logger
printAge() {
console.log(`I am ${this.age} years old`);
}
}
const user = new User("Ron", 25);
user.greet();
user.printAge();
One @logger tag. No duplicated console.log calls. Add a new method? Slap @logger on it. Done.
The trade-off
Decorators make cross-cutting concerns (logging, caching, validation) clean and reusable. But they hide behavior. Someone reading greet() won't see the logging unless they know to check the decorator. That's a readability cost.
Use them when the pattern is genuinely repeated. Don't use them to be clever.
Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.
Forward Deployed AI Engineering: From Customer Problem to Production Value
A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.
January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.
View the live cohortKeep reading
- TypeScript Anti-Patterns That Cost You Twice: Build Time, Runtime, and the Interview
- The Frontend Toolchain Is Now Written in Rust and Go. What That Means for You
- oxlint vs ESLint: Why I Replaced ESLint with oxlint
- SystemJS Is Dead. Native ESM Finally Won.
- Replace Axios with a Simple Custom Fetch Wrapper (Production-Ready Guide)
- Stop Shipping ChatGPT Wrappers. Ship an Agent in TypeScript, or Don't Bother.