I am trying to create a class that extends a Base class which provides helpers for .get() and similar.
My original idea was to have the constructor set the attributes of the class instead of having to call Object.assign everywhere. For some reason the mapper annotation does not permit arguments in the constructor (ZeroArgumentsConstructor). What is the reason for this?
As it is now I have to do some magic stuff that typescript isn't happy about. Here is a slim version:
class Base<T extends Base<T>> {
static create(attrs) {
return Object.assign(new this(), attrs);
}
put() {
return mapper.put(this);
}
}
@table('something')
class User extends Base<User> {
@hashKey()
email: string;
}
This valid in typescript but the issue is that the return type of create is any as you can't use generics in static neither can you use new this() in a already instanced object.
If there is a better way to do this I would happily do it. But this syntax seems way cleaner than the Object.assign pattern.
I am trying to create a class that extends a
Base
class which provides helpers for.get()
and similar. My original idea was to have the constructor set the attributes of the class instead of having to callObject.assign
everywhere. For some reason the mapper annotation does not permit arguments in the constructor (ZeroArgumentsConstructor
). What is the reason for this?As it is now I have to do some magic stuff that typescript isn't happy about. Here is a slim version:
This valid in typescript but the issue is that the return type of create is any as you can't use generics in static neither can you use
new this()
in a already instanced object.If there is a better way to do this I would happily do it. But this syntax seems way cleaner than the
Object.assign
pattern.