1

When you write a class A in Typescript, the type A is not actually the type of what the identifier A refers to in Javascript. A is the type of the instances of A. So, what is the type of A ?

Take this example :

class A {}

const B: ? = A;

const a = new B();

If we were to type B explicitly, what should we write to make this code compile ?

ostrebler
  • 940
  • 9
  • 32
  • 6
    This is not a joke: it's `typeof A`. – jcalz Feb 08 '20 at 01:48
  • @jcalz Now that was straightforward. Thank you. – ostrebler Feb 08 '20 at 01:51
  • 2
    See [this answer](https://stackoverflow.com/a/50396312/2887218) for more details about the difference between types and values and the confusion people have when the same name is used for a type and for a value. The type `typeof value` is always the type of a value named `value`. So `let x = "hey"; type X = typeof x;` is the same as `type X = string`. For class constructors, though, the compiler actually *reports* "`typeof A`". You could also use a constructor signature like `{new(): A}`. – jcalz Feb 08 '20 at 01:55

0 Answers0