Classes with Types
Prerequisites: Interfaces
TypeScript classes work like JavaScript classes, with types added to fields, parameters, and return values.
class User {
name: string;
role: string;
constructor(name: string, role: string = "member") {
this.name = name;
this.role = role;
}
isAdmin(): boolean {
return this.role === "admin";
}
}
const ada = new User("Ada", "admin");
console.log(ada.isAdmin());
Expected output
true
A class can also implements an interface, requiring it to provide
every property/method the interface declares.