In this lesson, you will learn how to extend a class's functionality through typescript decorators. Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.
class Lesson { lessonNumber: number = 0; } const lesson = new Lesson(); console.log(lesson.lessonNumber); // 0
Decorator:
function classDecorator<T extends { new (...args: any[]): {}}>(target: T) { return class extends target { lessonNumber = 3; toString() { return `${target.name}::${JSON.stringify(this, null, 2)}` } } } @classDecorator class Lesson { lessonNumber: number = 0; } const lesson = new Lesson(); console.log(lesson.lessonNumber); // 3 console.log(lesson.toString()) /** * "Lesson::{ "lessonNumber": 3 }" */