TypeScript Simple class


Example

class Car {
    public position: number = 0;
    private speed: number = 42;
    
    move() {
        this.position += this.speed;
    }
}    

In this example, we declare a simple class Car. The class has three members: a private property speed, a public property position and a public method move. Note that each member is public by default. That's why move() is public, even if we didn't use the public keyword.

var car = new Car();        // create an instance of Car
car.move();                 // call a method
console.log(car.position);  // access a public property