Classes

Classes

interface Point {
  x: number,
  y: number
}

class Point {
  x:number;
  private y:number;
  draw() {
    console.log(this.x);
  }
  constructor(x:number, y:number){
    this.x = x;
    this.y = y;
  }
}

let point = new Point(1, 2);
point.draw();
point.x;

Everything public, but use private when you need it

Can autogenerate fields and set it properly just by defining it in the constructor with private/public.

Properties

Simplify syntax, dont need both get and set.

Optional Parameters

Can't have multiple constructors or functions with different type definitions, so adding a question mark after a parameter makes it optional

Everything after the first optional parameter should be optional too

Abstract

  • abstract class must be extended, can't be instinated

  • abstract function must be defined in derived class

  • protected is private but can be used by derived classes

Last updated