TypeScript Abstract Classes
An abstract class can supply shared implementation while requiring subclasses to implement selected methods. It cannot be directly constructed in checked TypeScript code.
How it works
A collection summary shares a formatting method, but each concrete source supplies its count. This is a small example of a template with a required operation. An interface would be enough if no implementation needed sharing. Abstract restrictions are part of the checked source model; they are not a substitute for validating externally created objects.
Run the code
Try in playground loads this source without running it. Choose Run when ready. The existing TypeScript 5.8.3 worker transpiles and executes one file; it does not perform full semantic type checking, load npm imports or provide DOM/Node APIs. Use the local compiler for the type contract.
For a full check, save the three downloads together in a new folder, run npm install, then npm run check. Use npm run build followed by npm start to execute the checked JavaScript with Node. The supplied project enables strict checking, indexed-access checks and exact optional properties.
Example
abstract class CountSource {
abstract count(): number;
summary(): string { return this.count() + " packets"; }
}
class FixedSource extends CountSource { count(): number { return 14; } }
console.log(new FixedSource().summary());
Expected result
14 packets
FixedSource supplies the required count method and inherits summary. The base implementation assumes count returns a meaningful number, so concrete implementations still need behavioral tests. A badly implemented method returning a negative number would satisfy the type but violate the intended domain.