Skip to content

Instantly share code, notes, and snippets.

@ivankisyov
Last active December 25, 2018 16:04
Show Gist options
  • Save ivankisyov/d0e952b23b58f5a7149d06185efc913c to your computer and use it in GitHub Desktop.
Save ivankisyov/d0e952b23b58f5a7149d06185efc913c to your computer and use it in GitHub Desktop.
Classes in TypeScript

Classes in TypeScript

Types of properties

  • public(the default case)

  • private

    • you can only access it from the class. Cannot set/read it from outside.
    class Human {
      private type: string = "terran";
      constructor(public name: string) {
        this.name = name;
      }
    }
    const ivan = new Human("Ivan");
    
    // Error: Property 'type' is private and only accessible within class 'Human'
    console.log(ivan.type);
    
    class Human {
      private type: string = "terran";
      constructor(public name: string) {
        this.name = name;
      }
      public getType(): string {
        console.log(this.type);
        return this.type;
      }
    }
    const ivan = new Human("Ivan");
    
    // no error: the return value will be terran
    ivan.getType();
    
    
  • protected

    • behaves like private type and all objects that inherit can access those kind of properties

Getters and Setters

Example of using a getter

class User {
  private _membership = "standard";

  constructor(public name: string) {
    this.name = name;
  }

  set membership(value: string) {
    this._membership = value;
  }

  get membership(): string {
    return this._membership;
  }
}

const ivan = new User("Ivan");

const ivanMembership = ivan.membership;
console.log(ivanMembership); // standard

Example of using a setter

class User {
  private _membership = "standard";

  constructor(public name: string) {
    this.name = name;
  }

  set membership(value: string) {
    this._membership = value;
  }

  get membership(): string {
    return this._membership;
  }
}

// create the new user
const ivan = new User("Ivan");

// set user's membership
ivan.membership = "premium";

// get user's membership
const ivanMembership = ivan.membership;
console.log(ivanMembership);

The same setter example from above compiled to vanilla ES6

"use strict";
class User {
    constructor(name) {
        this.name = name;
        this._membership = "standard";
        this.name = name;
    }
    set membership(value) {
        this._membership = value;
    }
    get membership() {
        return this._membership;
    }
}
// create the new user
const ivan = new User("Ivan");
// set user's membership
ivan.membership = "premium";
// get user's membership
const ivanMembership = ivan.membership;
console.log(ivanMembership);

Static properties and methods

class Dashboard {
  static version = "1.0.0";
  static getVersion() {
    return this.version;
  }
}

console.log(`Version: ${Dashboard.version}`);

const currentVersion = Dashboard.getVersion();
console.log(currentVersion);

// the result from both logs will be: 1.0.0

Static properties and methods(compiled to vanilla ES6)

"use strict";
class Dashboard {
    static getVersion() {
        return this.version;
    }
}
Dashboard.version = "1.0.0";
console.log(`Version: ${Dashboard.version}`);
const currentVersion = Dashboard.getVersion();
console.log(currentVersion);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment