Using interfaces, classes, and inheritance - the Factory Design Pattern

To illustrate how we can use interfaces and classes in a large TypeScript project, we will take a quick look at a very well-known object-oriented design pattern--the Factory Design Pattern.

Business requirements

As an example, let's assume that our business analyst gives us the following requirements:

  1. You are required to categorize people, given their date of birth, into three different age groups---Infants, Children, and Adults.
  2. Indicate with a true or false flag whether they are of a legal age to sign a contract.
  3. A person is deemed to be an infant if they are less than 2 years old.
  4. Infants cannot sign contracts.
  5. A person is deemed to be a child if they are less than 18 years old. ...

Get Mastering TypeScript - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.