Tutorial by Examples

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 met...
class Car { public position: number = 0; protected speed: number = 42; move() { this.position += this.speed; } } class SelfDrivingCar extends Car { move() { // start moving around :-) super.move(); super.move(); } } ...
In this example we use the constructor to declare a public property position and a protected property speed in the base class. These properties are called Parameter properties. They let us declare a constructor parameter and a member in one place. One of the best things in TypeScript, is automatic ...
In this example, we modify the "Simple class" example to allow access to the speed property. Typescript accessors allow us to add additional code in getters or setters. class Car { public position: number = 0; private _speed: number = 42; private _MAX_SPEED = 100 ...
abstract class Machine { constructor(public manufacturer: string) { } // An abstract class can define methods of it's own, or... summary(): string { return `${this.manufacturer} makes this machine.`; } // Require inheriting classes to implement methods ...
Sometimes it's useful to be able to extend a class with new functions. For example let's suppose that a string should be converted to a camel case string. So we need to tell TypeScript, that String contains a function called toCamelCase, which returns a string. interface String { toCamelCase()...
Given a class SomeClass, let's see how the TypeScript is transpiled into JavaScript. TypeScript source class SomeClass { public static SomeStaticValue: string = "hello"; public someMemberValue: number = 15; private somePrivateValue: boolean = false; constructor ()...

Page 1 of 1