Closed brandonros closed 5 months ago
doesn't actually work. curious what I am doing wrong. not telling TypeScript that the T generic is a class?
I'm actually having the same error. Are you resolve this issue?
That's because generics are a Typescript only feature and get stripped at compile time, so you're essentially trying to pass something that doesn't exist to plainToClass
.
This is essentially what you're trying to do:
function getResourceById<T>(id: string): T { //the T generic gets stripped at compile time
//do stuff to get the resource
return plainToInstance(T, plainObject) //T is not defined
}
getResourceById<Quote>("abc")
What you should be doing is passing the Quote
class as a method parameter instead:
function getResourceById<T>(id: string, resourceType: ClassConstructor<T>): T {
//do stuff to get the resource
return plainToInstance(resourceType, plainObject)
}
getResourceById("abc", Quote)
Thank you @ElPerenza for your answer. Closing as invalid.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.