My approach involves using the concept of inheritance to initialize properties of a class<T>
with an Object
that implements all T
properties passed to the constructor
export default abstract class DatabaseObjectModel<T> extends JSONModel<T> {
constructor(json: T) {
super();
this.fromJSON(json);
}
}
export default abstract class JSONModel<T> extends ModelValidation {
// abstract toJSON(): Object;
// abstract fromJSON(json: Object): T;
toJSON() {
// Default toJSON
return JSON.parse(JSON.stringify(this));
}
fromJSON(json: Object) {
// For each key of json, assign value to this.key
for (let key in json) {
this[key] = json[key];
}
}
}
An issue arises when trying to initialize classes inheriting from DatabaseObjectModel
like so:
new ImplementingDatabaseObjectModel({
id: randomUUID(),
name: "MASTER",
permissions: [],
assignedRoles: undefined,
})
This results in the following error message:
Argument of type '{...}' is not assignable to parameter of type 'ImplementingDatabaseObjectModel'
I am seeking guidance on how to modify the constructor
of DatabaseObjectModel<T>
to accept an Object
that implements all properties of type T
, without requiring its instance.