We have a class with some users and we need to type it using interfaces.
class User {
name: string = '';
lastName: string = '';
age: number = 18;
isAdmin: boolean = false;
constructor(props) {
if (props?.name) this.name = props.name;
if (props?.lastName) this.lastName = props.lastName;
if (props?.age) this.age = props.age;
if (props?.isAdmin) this.isAdmin= props.isAdmin;
}
add(key, value) {
this[key] = value;
}
}
The first interface will describe the types that come to our constructor and will look like this:
interface IUser {
name?: string;
lastName?: string;
age?: number;
isAdmin?: boolean;
}
Types in the second interface must inherit from the type described in the IUser interface. How can we implement this?
New contributor
monkey789 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.