Classes

We should already be familiar with the basics of TypeScript classes, as we have declared some of them in previous chapters. We will now look at some details and OOP concepts through examples. Let's start by declaring a simple class:

class Person { 
    public name: string; 
    public surname: string; 
    public email: string; 
    public constructor( 
        name: string, surname: string, email: string 
    ) { 
        this.email = email; 
        this.name = name; 
        this.surname = surname; 
    } 
    public greet() { 
        console.log("Hi!"); 
    } 
} 

We use classes to represent the type of an object or entity. A class is composed of a name, properties (also known as attributes), and methods. Both methods and properties are known as members. Class properties are used to describe the object's characteristics, ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.