TypeScript, abstract classes, and constructors
Blog post from LogRocket
TypeScript's capability to define abstract classes is highlighted through a practical exploration of constructor usage, emphasizing that these classes cannot be instantiated directly and must be subclassed. The article demonstrates setting up a Node.js project with TypeScript to create a scratchpad for experimenting with abstract classes, detailing the process of configuring a TypeScript project and writing an abstract class with a constructor. It shows how to subclass this abstract class, either by not implementing a constructor, thereby using the base class's constructor, or by creating a new constructor that must call the base class constructor with appropriate arguments. The nuances of TypeScript enforcing these rules are illustrated by the errors encountered when incorrect instantiations are attempted. The discussion underscores how TypeScript's abstract class concept translates into valid JavaScript despite compilation errors in TypeScript, reminding readers that these are TypeScript-specific constructs.