Classes

The reason why it’s important to have classes in JavaScript as a first class item is that:

  1. Classes offer a useful structural abstraction
  2. Provides a consistent way for developers to use classes instead of every framework (emberjs,reactjs etc) coming up with their own version.
  3. Object Oriented Developers already understand classes.

Finally JavaScript developers can have class. Here we have a basic class called Point:

  1. class Point {
  2. x: number;
  3. y: number;
  4. constructor(x: number, y: number) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. add(point: Point) {
  9. return new Point(this.x + point.x, this.y + point.y);
  10. }
  11. }
  12. var p1 = new Point(0, 10);
  13. var p2 = new Point(10, 20);
  14. var p3 = p1.add(p2); // {x:10,y:30}

This class generates the following JavaScript on ES5 emit:

  1. var Point = (function () {
  2. function Point(x, y) {
  3. this.x = x;
  4. this.y = y;
  5. }
  6. Point.prototype.add = function (point) {
  7. return new Point(this.x + point.x, this.y + point.y);
  8. };
  9. return Point;
  10. })();

This is a fairly idiomatic traditional JavaScript class pattern now as a first class language construct.

Inheritance

Classes in TypeScript (like other languages) support single inheritance using the extends keyword as shown below:

  1. class Point3D extends Point {
  2. z: number;
  3. constructor(x: number, y: number, z: number) {
  4. super(x, y);
  5. this.z = z;
  6. }
  7. add(point: Point3D) {
  8. var point2D = super.add(point);
  9. return new Point3D(point2D.x, point2D.y, this.z + point.z);
  10. }
  11. }

If you have a constructor in your class then you must call the parent constructor from your constructor (TypeScript will point this out to you). This ensures that the stuff that it needs to set on this gets set. Followed by the call to super you can add any additional stuff you want to do in your constructor (here we add another member z).

Note that you override parent member functions easily (here we override add) and still use the functionality of the super class in your members (using super. syntax).

Statics

TypeScript classes support static properties that are shared by all instances of the class. A natural place to put (and access) them is on the class itself and that is what TypeScript does:

  1. class Something {
  2. static instances = 0;
  3. constructor() {
  4. Something.instances++;
  5. }
  6. }
  7. var s1 = new Something();
  8. var s2 = new Something();
  9. console.log(Something.instances); // 2

You can have static members as well as static functions.

Access Modifiers

TypeScript supports access modifiers public,private and protected which determine the accessibility of a class member as shown below:

accessible on public protected private
class yes yes yes
class children yes yes no
class instances yes no no

If an access modifier is not specified it is implicitly public as that matches the convenient nature of JavaScript ?.

Note that at runtime (in the generated JS) these have no significance but will give you compile time errors if you use them incorrectly. An example of each is shown below:

  1. class FooBase {
  2. public x: number;
  3. private y: number;
  4. protected z: number;
  5. }
  6. // EFFECT ON INSTANCES
  7. var foo = new FooBase();
  8. foo.x; // okay
  9. foo.y; // ERROR : private
  10. foo.z; // ERROR : protected
  11. // EFFECT ON CHILD CLASSES
  12. class FooChild extends FooBase {
  13. constructor() {
  14. super();
  15. this.x; // okay
  16. this.y; // ERROR: private
  17. this.z; // okay
  18. }
  19. }

As always these modifiers work for both member properties and member functions.

Abstract

abstract can be thought of as an access modifier. We present it separately because opposed to the previously mentioned modifiers it can be on a class as well as any member of the class. Having an abstract modifier primarily means that such functionality cannot be directly invoked and a child class must provide the functionality.

  • abstract classes cannot be directly instantiated. Instead the user must create some class that inherits from the abstract class.
  • abstract members cannot be directly accessed and a child class must provide the functionality.

Constructor is optional

The class does not need to have a constructor. e.g. the following is perfectly fine.

  1. class Foo {}
  2. var foo = new Foo();

Define using constructor

Having a member in a class and initializing it like below:

  1. class Foo {
  2. x: number;
  3. constructor(x:number) {
  4. this.x = x;
  5. }
  6. }

is such a common pattern that TypeScript provides a shorthand where you can prefix the member with an access modifier and it is automatically declared on the class and copied from the constructor. So the previous example can be re-written as (notice public x:number):

  1. class Foo {
  2. constructor(public x:number) {
  3. }
  4. }

Property initializer

This is a nifty feature supported by TypeScript (from ES7 actually). You can initialize any member of the class outside the class constructor, useful to provide default (notice members = [])

  1. class Foo {
  2. members = []; // Initialize directly
  3. add(x) {
  4. this.members.push(x);
  5. }
  6. }