import { Model } from "../../../lib/db/Model";
export enum EUserRole {
admin,
teacher,
user,
}
export class UserModel extends Model {
name: string;
phoneNo: number;
role: EUserRole;
createdAt: Date;
constructor({
name,
phoneNo,
role,
createdAt,
}: {
name: string;
phoneNo: number;
role: EUserRole;
createdAt: Date;
}) {
super();
this.name = name;
this.phoneNo = phoneNo;
this.role = role;
this.createdAt = createdAt;
}
}
Is there any shortcut way to write the above code?And does typescript has the concept called named constructor?
ignore - It looks like your post is mostly code; please add some more details.
CodePudding user response:
You can use Object.assign
and bleed all properties to this
as property names are same
import { Model } from "../../../lib/db/Model";
export enum EUserRole {
admin,
teacher,
user,
}
export interface IUserModel {
name: string;
phoneNo: number;
role: EUserRole;
createdAt: Date;
}
export class UserModel extends Model {
name: string;
phoneNo: number;
role: EUserRole;
createdAt: Date;
constructor(userModel: IUserModel) {
super();
Object.assign(this, userModel)
}
}