Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
338 stars 11 forks source link

[Question] How to get field type inside a decorator #86

Closed Crash1988 closed 1 year ago

Crash1988 commented 1 year ago

I am trying to use this package at runtime inside a field decorator. I need to know that attr1 is type Number. any help how ?

import { getType } from "tst-reflect";

function logType(target : any, key : string) {
     const type = getType(target[key]);
     console.log(type); // i am trying to get what type is the field attr1
}

class Demo{ 
    @logType 
    public attr1 : number;

}
Hookyns commented 1 year ago

Hi @Crash1988,

this is a way how to do it in the current version: https://stackblitz.com/edit/tst-reflect-property-decorator?file=index.ts

Hookyns commented 1 year ago

I'm closing this. Feel free to reopen in case you'll face some issues with this solution.