selmertsxの素振り日記

ひたすら日々の素振り内容を書き続けるだけの日記

TypeScriptにおいて継承先のclassのプロパティはオブジェクトの生成時には利用できない

概要

表題のとおり。継承先のプロパティは、オブジェクトの生成時には利用できなかった。 このあたりの仕組みが気になる。やり方があれば、ご紹介頂けると幸いです!

確認のコード

class A {
  protected hoge = 3;
  constructor () {
    console.log(this.hoge);
    this.hoge;
  }

  hage() {
    return this.hoge;
  }
}

class B extends A {
  protected hoge = 4;
}
console.log("-------construct-----");
const a = new A();
const b = new B();
console.log("-------call function-----");
console.log(a.hage());
console.log(b.hage());
➜  typescript ts-node override.ts
-------construct-----
3
3
-------call function-----
3
4