This works in Javascript and Typescript:
class A { /* ... */ }
const B = class extends A { /* ... */ }
var x = new B();
console.log(x instanceof B, x.constructor.name); // true B
But if I try to declare the type of x as B:
var x: B = new B();
I get Typescript Error:
'B' refers to a value, but is being used as a type here. Did you mean 'typeof B'?
(Note, I also get the same error if I replace const B = class extends A { /* ... */ } with simply const B = A, which is what I had originally to make things as simple as possible, but updated on review).
I don't really understand why this should be the case. According to runtime Javascript x is a B (as shown in the console.log above). And all classes are objects ("values") under the hood anyway (the Typescript docs themselves equate classes to "constuctor objects"). I'm guessing it's just a limitation of Typescript static analysis - it can't figure out that "B" is a constructor object just like "A" is and track down its typing?
And also, I don't think so, but wondering, is there actually a way to have this work in Typescript - know B is a constructor object like A and allow the use of B as a type??
P.S. I realize there are many questions on SO relating to "Blah refers to a value, but is being used as a type here. Did you mean 'typeof Blah'?", but I couldn't find Q&A approaching it as directly as above. Apologies if I missed it.
console.log(x instanceof B, x.constructor.name === 'B'); // true trueactually returns "true false" if not compiled as typescript. So "this works in both TypeScript and JavaScript" is a bit misleading. If it returns "true true" when compiled the compiler is doing something wrong.true falsein TypeScript also. The question is just in error, and that line should probably be removed.