lundi 3 juillet 2023

How can I find the defined type of a property/field at runtime in TypeScript?

I am trying to get the type of a property/field in TypeScript. Specifically, from the property decorator. The property does not have a value set so I can not derive it from the type of the value. Consider an example

class Example {
  abc: ABC
}

How would I get the class/class name of the property? To be specific, not the JavaScript 'primitive' type given by typeof x but the actual class(name), in this case ABC.

I've tried using reflect-metadata but it doesn't look like it's retaining the defined type in the metadata, running the following code:

console.log(Reflect.getMetadataKeys(target) + ", " + Reflect.getMetadata("design:type", target, propertyKey)

// -> ", undefined"

So it doesn't seem like there is any metadata relating to the type defined, or at all for that matter.





Aucun commentaire:

Enregistrer un commentaire