I don't know how to type a variable to contain a derived class that will be instantiated when needed.
// I have a base class... abstract class Base { constructor() { console.log("Base"); } abstract fun(); } // ...for various derived classes class Derived extends Base { constructor() { super(); console.log("Derived"); } fun() { console.log("Have fun") } } // Now I want to collect the derived classes in an array... interface OneNode { handler: Base; // What should I put here? Obviously if I put "any" don't have complains } const availableNodes: OneNode[] = [ {handler: Derived} // Here ts complains that "Property 'fun' is missing in type 'typeof Derived' but required in type 'Base'. ts(2741)" ] // ...and instantiate some of them only when needed const y = new availableNodes[0].handler(); // Here ts complains that "This expression is not constructable. Type 'Base' has no construct signatures. ts(2351)" y.fun(); If I ignore the TypeScript complains or type the "handler" field as "any", everything works as expected. But I want to properly type the "handler" variable.
{handler: new Derived()}? You are assigning the class itself, not an instance. A class is not an instance of itself nor its parent.const node: OneNode = Derivedwill fail the same way.const node: OneNode = new Derived()will work just fine.new() => Baseas shown in this playground link. Does that fully address the question? If so then I'll write an answer (or, more likely, find a duplicate). If not, then what's missing?