TypeScript enhances JavaScript classes by adding type annotations and visibility modifiers.
The members of a class (properties and methods) are typed using type annotations, much like variables.
class Person { name: string; } const person = new Person(); person.name = “Jane”; |
Class members can also have special modifiers that influence their visibility.
TypeScript has three main visibility modifiers:
|
class Person { private name: string; public constructor(name: string) { this.name = name; } public getName(): string { return this.name; } } const person = new Person(“Jane”); console.log(person.getName()); // person.name isn’t accessible from outside the class since it’s private |