Closed avin-kavish closed 1 year ago
@avin-kavish
It works for me.
import { getType } from "tst-reflect";
function entity(_: any)
{
}
@entity
export class Cat
{
id = 0;
toys: Toy[] = [];
}
@entity
export class Toy
{
id = 0;
}
const type = getType<Toy>();
type.getCtor().then(ctor => console.log(ctor)); // It prints [class Toy]; working with Cat class too.
Can you send repro, eg. on StackBlitz?
What I did is more like this. The link is not running but I think you can check the generated code.
Okay, there are three issues.
First issue is not generated "ctor" property.
Second issue is how decorators work.
There is __decorate
call inside generated TypeScript which is called before type is exported from the module so you cannot import it from the decorator. This is a "no go".
You will receive type, there is no problem, but import is gonna return undefined
, always.
Solution is simple. Use decorator's parameter. First parameter is the decorated class.
/**
* @reflect
*/
export function entity<TType extends Function>(Class: TType) {
const typeT = getType<TType>();
console.log(typeT.name, Class);
}
I've already fixed the issue with decorators (in tst-reflect-transformer@0.9.13
) so you can use the solution from the second point.
Meanwhile, I will fix the issue from the first point, but you cannot use it in decorator anyway.
Alright. Actually, 2nd point is not a problem. I did it that way just for the stackblitz example. In my actual implementation I push it to a container and later once all the modules are loaded, I check the types.
/**
* @reflect
*/
export function entity<TType extends Constructor>(_: any) {
const typeInfo = getType<TType>()
EntityTypeStore.push(typeInfo)
}
It's happening here also. https://stackblitz.com/edit/tst-reflect-issue-49-htnd32?file=package.json,index.ts
Happened again. This time seemingly random, class next to it gets imported just fine.
Fixed in v1. Not in the current version.
This doesn't work,
This works,
It's the
toys: Toy[] = []
line causing the issue, it works when I comment it out. It looks like it only happens for array types,Toy[]
. just a direct reference works fine.