Classes
Classes in TypeScript add public, private, protected and readonly. Parameter properties declare and assign fields in the constructor. Classes can implement interfaces.
Example
interface Describes { describe(): string }
class Student implements Describes {
constructor(public name: string, private score: number = 0) {}
add(p: number): this { this.score += p; return this; }
describe(): string { return `${this.name}: ${this.score}`; }
}
console.log(new Student("Ada", 70).add(5).describe());Result
Ada: 75
Exercise
Hide a member from outside.
score: number;Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.