Getting a property type using reflect-metadata

198 views Asked by At

I have a number of classes that all have a body property, e.g:

class MyClass {
  body: {
    name: string,
    address: string,
    favouriteThings: Array<string>
  }
}

I'd like to have an automated way of getting the type of the body object, so I can build a JSON schema from them, however, if I do:

const MyPropertyDecorator = (): PropertyDecorator => {
  return (target, property) => {
    const t = Reflect.getMetadata('design:type', target, property)
    console.log(`${property} type: ${t.name}`)
  }
}

class MyClass {
  @MyPropertyDecorator()
  body: {
    name: string,
    address: string,
    favouriteThings: Array<string>
  }
}

I see this on boot:

body type: Object

Is there any way I can get more information about the property type at build time? Or are there any alternative approaches that I haven't thought of?

0

There are 0 answers