Question presented to candidate:
"What is an abstract class in TypeScript? When would you reach for one instead of an interface, and what does the abstract keyword actually do at run time?"
What a strong answer should cover:
- An abstract class cannot be instantiated with
new; it exists to be extended, and it can mix finished code (fields, constructor, concrete methods) withabstractmembers that have no body - Every non-abstract subclass must implement all abstract members, or tsc reports an error; a subclass can also stay abstract and pass the duty down
- The template method pattern: a concrete base method calls abstract hooks that each subclass fills in
- Abstract class vs interface: the class carries shared code and state and survives to run time (
instanceofworks), an interface is a pure shape with no code and is erased abstractitself is compile-time only: the emitted JavaScript is an ordinary class, so the "cannot instantiate" rule is a type check, not a run-time guard- Constructor types:
new (...) => Trejects an abstract class,abstract new (...) => T(TypeScript 4.2) accepts it, which is what mixins over abstract bases need
Code / implementation expected: Yes β a small abstract base class with one abstract method, two subclasses, and a concrete method that uses the abstract one.