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

16 messages · Page 1 of 1 (latest)

hoary stream
#

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.

lavish folio
#

only decorated classes members get metadata

#

also note that decorator metadata is deprecated and will eventually be removed

#

there is no migration path, as far as I am aware

hoary stream
#

ill just pass in the type ig

#

the prototype

#

decorators wont get removed as a whole right?

lavish folio
#

new decorators are unlikely to be removed and are not deprecated

hoary stream
#

are new decorators the ones with the ClassDecoratorContext

lavish folio
#

I'm not familiar with that type name

#

new decorators are enabled by default, and are disabled by the --experimentalDecorators flag

hoary stream
#

will this type of decorator get removed

lavish folio
#

the two different decorator proposals overlap somewhat

#

I believe that'll work in both