-
Notifications
You must be signed in to change notification settings - Fork 185
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
Losing metadata #41
Comments
When you decorate a non-static property, method, or accessor, the When you decorate a static property, method, or accessor, the When you decorate a class, the The The Here are some other examples: function info(target: any, property?: PropertyKey, descriptor?: PropertyDescriptor) {
const data: string[] = [];
if (arguments.length === 1) {
data.push("class");
}
else {
if (typeof target === "function") {
data.push("static");
}
else {
data.push("non-static");
}
if (!descriptor) {
data.push("field");
}
else if (descriptor.get || descriptor.set) {
data.push("accessor");
}
else {
data.push("method");
}
data.push(JSON.stringify(propertyKey));
}
console.log(data.join(" "));
}
@info // logs: class
class Sample {
@info static a = 1; // logs: static field "a"
@info static b() {} // logs: static method "b"
@info static get c() { return 1; } // logs: static accessor "c"
@info x = 1; // logs: non-static field "x"
@info y() {} // logs: non-static method "y"
@info get z() { return 1; } // logs: non-static accessor "z"
} With the Reflect.getMetadata(metadataKey, instance, memberName); If you want to get metadata for a static member, you can do: Reflect.getMetadata(metadataKey, Field, memberName); If you want to get metadata for a class, you can do: Reflect.getMetadata(metadataKey, Field); |
@rbuckton Thank you for this. This is very helpful. So, if I want to collect all the metadata on the constructor, I just have to check what I'm getting. If it's a constructor use that, otherwise use the constructor passed to me? It's for an orm. In the end I want to have a mapping key with an object that describes everything in the class. Does that make sense, or am I then abusing metadata? |
Hello,
I've been trying to use this module using typescript for the decorators. I seem to keep losing my data and it's confusing me a little bit. Upon creating a small test, I found some odd behavior I wasn't expecting (I assume the problem here is me).
Whenever I put a decorator on a property, the decorator doesn't get the class as target. I have to use
target.constructor
to get it. But when I put the decorator on the class itself, it does seem to get the class.Code speaks a thousand words, so:
This confuses me. How can I always get the metadata of a class (including properties) if I always get a different target? Am I missing something on how this should be used?
Thank you for your time.
The text was updated successfully, but these errors were encountered: