Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type reference to constructor of a class in Typescript?

Tags:

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
like image 447
Joon Avatar asked Oct 29 '25 14:10

Joon


1 Answers

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
like image 81
Titian Cernicova-Dragomir Avatar answered Oct 31 '25 05:10

Titian Cernicova-Dragomir