Joon
Joon

Reputation: 9914

Type reference to constructor of a class in Typescript?

I want to reference the type of a class constructor itself, not its instance.

class A {
  static a = 1;
  b = 2;
}

let a: A; // a is an instance of A
a.b === 2 // this works as expected

let aClass: A['constructor'];
aClass.a // not found in Typescript

Upvotes: 4

Views: 874

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250366

You can use typeof A to reference the class itself and not an instance of the class, and the class is represented by the constructor

let aClass: typeof A =A
aClass.a //ok

Upvotes: 5

Related Questions