-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: Provide additional context about the class to member decorators #466
Comments
I want to highlight that is a blocker for a lot of use cases, including several existing popular ORMs (MikroORM, TypeORM etc.). |
@rbuckton Actually, we can retrieve the class name from the decorated method. Here's a demonstration: function Log(target, context) {
return function (...args) {
const prototype = Object.getPrototypeOf(this);
const Class = prototype.constructor;
const className = Class.name;
console.debug(`${className}.${context.name}:`, ...args);
return target.apply(this, args);
}
}
class Test {
@Log
add(x, y) {
return x + y;
}
}
describe('Test @Log decorator for class methods', () => {
test('should work', () => {
const t = new Test();
t.add(1, 2);
});
}); |
@Haixing-Hu What about the field decorators? Many ORMs (and some validation libraries) collect and save metadata using decorators on class fields and they need access to the class and field names. |
That would give you the class name of the instance constructor, not the class name of the class it was declared on, and requires method invocation to access. You could use |
Member decorators (method, accessor, field, etc.) in the current proposal do not have access to the class constructor or prototype, and thus have no access to the name of the class containing the decorated element. This would be valuable information for something like a
@logged
decorator.I would propose the addition of either a
className
property to the context, or something like theclass
property below:Related: #465
The text was updated successfully, but these errors were encountered: