Klasės pavyzdys:

class Typescript {
 version: string
 constructor(version: string) {
  this.version = version
 }
 info(name: string) {
  return `[${name}]: Typescript version is ${this.version}`
 }
}

const ts = new Typescript('15')
console.log(ts.info('TS15'))

Standartinis:

readonly - reikšme galima keisti tik per konstruktorių

class Car {
 readonly model: string
 readonly numberOfWheels: number = 4

 constructor(theModel: string) {
  this.model = theModel
 }
}

Trumpesnis analogiškas variantas:

class Car {
 readonly numberOfWheel : number = 4
 constructor(readonly model: string) {}
}

Klasė:

class Animal {
 protected voice: string = ''
 public color: string = 'black'
 constructor() {
  this.go()
 }

 private go() {
  console.log('Go')
 }
}

private - tiesiogiai keisti neleidžiama, bet leidžiama per modifikatorius (get, set), tik klasė kurioje aprašytas. vs codas neišmes pasiūlymo apie tokio kitnamojo egzistavimą

public - galima keisti tiesiogiai

protected - tik klasės ir vaiku klasės viduje (pakeisti tik per funkciją), protected gali buti funcijos

class Cat extends Animal {
 public setVoice(voice: string): void {
  this.voice = voice
 }
}


const cat = new Cat()
cat.setVoice('test')
console.log(cat.color)

Abstrakti klasė - nuo jų kiti paveldi (metodu ir parametrus) - nekompiluojami

abstract class Component {
 abstract render(): void
 abstract info(): string
}

class AppComponent extends Component {
 render(): void {
  console.log('Component on render')
 }

 info(): string {
  return 'This is info'
 }
}

Rezultatas:

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };

    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };

})();

var Component = /** @class */ (function () {
    function Component() {
    }

    return Component;

}());

var AppComponent = /** @class */ (function (_super) {
    __extends(AppComponent, _super);
    function AppComponent() {
        return _super !== null && _super.apply(this, arguments) || this;
    }

    AppComponent.prototype.render = function () {
        console.log('Component on render');
    };

    AppComponent.prototype.info = function () {
        return 'This is info';
    };

    return AppComponent;

}(Component));

Jeigu nenorime butinai inicializuoti klasėje

class Point {
  x!: number;
  y!: number;
}
class Point {
  x: number
  y: number

  constructor(x: number, y: number){
    this.x = x
    this.y = y
  }

  a(name?: string){
  }
}

class D3Point extends Point (
  z: number
  constructor(x: number, y: number, z: number){
  super (x, y)
  this.z = z
  }
}

const point = new D3Point(1,1,1) - visi kint
class StaticTest = {
  static c = 'sdf'
}

const d = StaticTest.c

Abstrakčios klasės - neleidžia sukurti instance, bet leidžia extendinti

abstract class Test {
  myMethod() {
  }
}

new Test() -> negalima

class Testas extends Test {
}

new Testas() -> ok

Implementacija

interface C {
  test: () => void
}

class D implements C {
  test() {
}
}

tikrins ar klasės turi metoda

kai norima isitikinti kad klases turi tam tikra metoda (butina sekantiems veiksmam)