I've two generic functions:
findOne<Entity>(cls: ClassType<Entity>, id: number): Promise<Entity>;
plainToClass<T, V>(cls: ClassType<T>, plain: V, options?: ClassTransformOptions): T;
How can I forward the template from findOne()
to plainToClass()
like in this case?
findOne<Entity>(cls: ClassType<Entity>, id: number): Promise<Entity> {
return new Promise<Entity>(resolve => {
resolve(plainToClass(cls /* here */, this.electronService.ipcRenderer.sendSync('findOne', cls.name, id)));
});
}
Edit
Here is a full example: https://stackblitz.com/edit/typescript-lqnedb
ClassType
? What isClassTransformOptions
? What isthis.electronService.ipRenderer.sendSync()
? Do you want=>
instead of=
?) Good luck!findOne(User, 4)
callplainToClass(User /* <-- User here? */, ...)
? I forgot to precise that I'm totally new to TS. I'll add a full working example. Thanksnew Promise(r => r(foo(x)))
callsfoo(x)
. Have you tested it or tried to debug it? The issue in your linked code is not thatplainToClass()
is not being called; it's that it doesn't know how to turncreatedAt
into aDate
, as described here.