0

From TS

type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;

Say, I have a class Foo,

class Foo {}

InstanceType<Foo> gives never, and InstanceType<typeof Foo> gives Foo. So my question is why Foo is not treated as new-able thing?

ABOS
  • 3,723
  • 3
  • 17
  • 23
  • 2
    Because the *type* named `Foo` describes an instance of the class (if you want a value of type `Foo` you would be unhappy if someone gave you the class constructor, right?), while the *value* named `Foo` is the class constructor, whose type is not `Foo`, but `typeof Foo`, essentially the same as `new()=>Foo`. – jcalz Dec 12 '20 at 14:53
  • The answer [here](https://stackoverflow.com/questions/50376977/generic-type-to-get-enum-keys-as-union-string-in-typescript/50396312#50396312) explains this, but I guess the question isn’t exactly a duplicate. There’s probably an existing question somewhere though; still looking... – jcalz Dec 12 '20 at 14:57

0 Answers0