Lesson 23 of 80
A function that wraps a class or its members to add behavior declaratively — the higher-order-function idea again, this time expressed as an annotation instead of an explicit wrapping call.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
Structurally, what does `@logged` on a class method have in common with a React Higher-Order Component?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
function logged(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`Calling ${propertyKey}`);
return original.apply(this, args);
};
}
class Service {
@logged
fetchData() { /* ... */ }
}
@logged is sugar for "wrap fetchData with the logged function" — structurally, this is the exact same idea as a Higher-Order Component or a withUser-style HOC from the React track: a function that takes something (here, a method) and returns an enhanced version of it. The @ syntax just lets you apply that wrapping declaratively, right where the method is defined, instead of calling a wrapper function separately afterward.
@logged class ApiClient { ... } — a function that wraps a class definition.
experimentalDecorators (the older stage-2 proposal TypeScript has long supported) or targeting a recent enough spec for the newer, now-standardized decorators proposal — the two versions have real syntax and behavior differences, and mixing assumptions between them is a common source of confusion.@Column({ nullable: true }) is calling Column(...), which itself returns the actual decorator function applied to the property.If you've worked with Angular, NestJS, or TypeORM, decorators come up constantly — recognizing them as "a higher-order function applied via declarative syntax" rather than mysterious framework magic is what separates using them from actually understanding them.