'Model class constructor in Typescript
I am new to typescript and I am trying to create a "model" class.
The constructor should accept a list of properties (that come from the database), and any of them should be optional.
Here is the code so far:
export type UserRole = "admin" | "moderator" | "user" | "visitor";
export default class User{
public id: number | null = null;
public login: string = '';
public email: string = '';
public role: UserRole = 'visitor';
...
constructor({id, login, email, role, ... }){
this.id = id;
this.login = login;
this.email = email;
this.role = role;
....
}
As you can see, it doesn't look right. A lot of code is duplicated. And if I want to make the properties optional it will duplicate even more code:(
Can anyone point me in the right direction? thanks
Solution 1:[1]
I would suggest to use following utility type from here:
type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K
}[keyof T];
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
This will create a type out of all properties of a class without the methods.
You can use it like this:
export type UserRole = "admin" | "moderator" | "user" | "visitor";
export default class User{
public id: number | null = null;
public login: string = '';
public email: string = '';
public role: UserRole = 'visitor';
constructor({id, login, email, role }: NonFunctionProperties<User>){
this.id = id;
this.login = login;
this.email = email;
this.role = role
}
}
To make them all optional, just add Partial:
constructor({id, login, email, role }: Partial<NonFunctionProperties<User>>)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tobias S. |
