1.6 Classes

JavaScript practice has two very common design patterns: the module pattern and the class pattern. Roughly speaking, the module pattern uses closures to hide names and to encapsulate private data, while the class pattern uses prototype chains to implement many variations on object-oriented inheritance mechanisms. Libraries such as ‘prototype.js’ are typical of this practice. TypeScript’s namespaces are a formalization of the module pattern. (The term “module pattern” is somewhat unfortunate now that ECMAScript 2015 formally supports modules in a manner different from what the module pattern prescribes. For this reason, TypeScript uses the term “namespace” for its formalization of the module pattern.)

This section and the namespace section below will show how TypeScript emits consistent, idiomatic JavaScript when emitting ECMAScript 3 or 5 compliant code for classes and namespaces. The goal of TypeScript’s translation is to emit exactly what a programmer would type when implementing a class or namespace unaided by a tool. This section will also describe how TypeScript infers a type for each class declaration. We’ll start with a simple BankAccount class.

  1. class BankAccount {
  2. balance = 0;
  3. deposit(credit: number) {
  4. this.balance += credit;
  5. return this.balance;
  6. }
  7. }

This class generates the following JavaScript code.

  1. var BankAccount = (function () {
  2. function BankAccount() {
  3. this.balance = 0;
  4. }
  5. BankAccount.prototype.deposit = function(credit) {
  6. this.balance += credit;
  7. return this.balance;
  8. };
  9. return BankAccount;
  10. })();

This TypeScript class declaration creates a variable named ‘BankAccount’ whose value is the constructor function for ‘BankAccount’ instances. This declaration also creates an instance type of the same name. If we were to write this type as an interface it would look like the following.

  1. interface BankAccount {
  2. balance: number;
  3. deposit(credit: number): number;
  4. }

If we were to write out the function type declaration for the ‘BankAccount’ constructor variable, it would have the following form.

  1. var BankAccount: new() => BankAccount;

The function signature is prefixed with the keyword ‘new’ indicating that the ‘BankAccount’ function must be called as a constructor. It is possible for a function’s type to have both call and constructor signatures. For example, the type of the built-in JavaScript Date object includes both kinds of signatures.

If we want to start our bank account with an initial balance, we can add to the ‘BankAccount’ class a constructor declaration.

  1. class BankAccount {
  2. balance: number;
  3. constructor(initially: number) {
  4. this.balance = initially;
  5. }
  6. deposit(credit: number) {
  7. this.balance += credit;
  8. return this.balance;
  9. }
  10. }

This version of the ‘BankAccount’ class requires us to introduce a constructor parameter and then assign it to the ‘balance’ field. To simplify this common case, TypeScript accepts the following shorthand syntax.

  1. class BankAccount {
  2. constructor(public balance: number) {
  3. }
  4. deposit(credit: number) {
  5. this.balance += credit;
  6. return this.balance;
  7. }
  8. }

The ‘public’ keyword denotes that the constructor parameter is to be retained as a field. Public is the default accessibility for class members, but a programmer can also specify private or protected accessibility for a class member. Accessibility is a design-time construct; it is enforced during static type checking but does not imply any runtime enforcement.

TypeScript classes also support inheritance, as in the following example.

  1. class CheckingAccount extends BankAccount {
  2. constructor(balance: number) {
  3. super(balance);
  4. }
  5. writeCheck(debit: number) {
  6. this.balance -= debit;
  7. }
  8. }

In this example, the class ‘CheckingAccount’ derives from class ‘BankAccount’. The constructor for ‘CheckingAccount’ calls the constructor for class ‘BankAccount’ using the ‘super’ keyword. In the emitted JavaScript code, the prototype of ‘CheckingAccount’ will chain to the prototype of ‘BankAccount’.

TypeScript classes may also specify static members. Static class members become properties of the class constructor.

Section 8 provides additional information about classes.