Method and Property Decorators
Decorate individual methods and properties.
Method and Property Decorators is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
Introduction
Core Concept
@sealed
class BugReport {
type = 'report';
title: string;
constructor(t: string) { this.title = t; }
}Class Decorator
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}Method Decorator
function log(target: any, key: string, desc: PropertyDescriptor) {
const orig = desc.value;
desc.value = function(...args: any[]) {
console.log('Calling', key);
return orig.apply(this, args);
};
}Property Decorator
function readonly(target: any, key: string) {
Object.defineProperty(target, key, { writable: false });
}Parameter Decorator
function required(target: any, key: string, index: number) {
const meta = Reflect.getMetadata('required', target, key) || [];
meta.push(index);
Reflect.defineMetadata('required', meta, target, key);
}Decorator Factories
function role(name: string) {
return function(target: any, key: string) {
Reflect.defineMetadata('role', name, target, key);
};
}
class UserController {
@role('admin')
deleteUser() {}
}NestJS Decorators
@Controller('users')
class UsersController {
constructor(private usersService: UsersService) {}
@Get()
findAll(): User[] {
return this.usersService.findAll();
}
}reflect-metadata
import 'reflect-metadata';
// Enable in tsconfig: experimentalDecorators: true, emitDecoratorMetadata: trueDecorator Order
@first
@second
class Example {}
// Applied: second first, then firstQuick Check
Recap
Frequently asked questions
Is the “Method and Property Decorators” lesson free?
Yes — the full text of “Method and Property Decorators” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Method and Property Decorators”?
Decorate individual methods and properties. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Method and Property Decorators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Class Decorators: Wrapping Behavior
- Method and Property Decorators
- Parameter Decorators and Metadata
- Decorators in Frameworks: NestJS Example