I have a dilemma with my constructor that assigns properties to the instance:
class BaseModel {
constructor (args = {}) {
for (let key in args) {
this[key] = args[key]
}
}
}
class User extends BaseModel {
name: string
}
For creating instances, I've been using the following method:
let user = new User({name: 'Jon'})
However, now I'm interested in implementing class-transformer and replacing the basics with it:
let user = plainToClass(User, {name: 'Jon'})
Since my codebase heavily relies on the initial approach, I want to integrate the new method seamlessly without disrupting existing code:
constructor (args = {}) {
let instance = plainToClass(CLASSTYPE, args)
for (let key in Object.keys(instance)) {
this[key] = instance [key]
}
}
The challenge is identifying the class type within the constructor. Using "User" isn't feasible since there are other classes extending from the base model.