TypeScript Intermediate: Generics and Utility Types
Interfaces and Classes
In this lesson you will
- Extend an interface and combine types with intersections
- Write a class with access modifiers and parameter properties
- Make a class implement an interface
Introduction
In the first course you described object shapes with type and interface. Real programs build on those shapes: one thing is a more specific version of another, and classes bundle data with behaviour. This lesson covers both.
Extending an interface
extends builds a new interface on top of an existing one.
A Student has everything a Person has, plus more. Anywhere a Person is expected, you can pass a Student.
Intersections: combining types
With type, combine shapes using & (an intersection): the value must satisfy both.
Quick quiz
If interface Student extends Person, which statement is true?
Classes
A class is a blueprint for objects, with data and methods together. TypeScript adds types to the fields, and access modifiers:
public: anyone can use it (the default).private: only code inside the class can use it.protected: the class and its subclasses.readonly: can be set in the constructor, then never changed.
private is checked by TypeScript while you write. For protection that survives at runtime, JavaScript has its own #field syntax.
Parameter properties: a shortcut
Putting a modifier on a constructor parameter declares and assigns the field in one go. The class below is the same as writing out the fields by hand.
Implementing an interface
An interface can describe what a class must provide. implements makes TypeScript check it.
The array holds different classes, but code only relies on the Shape promise. If Square forgot area, the compiler would say so.
Inheritance and abstract classes
A class can extend another and reuse it. An abstract class cannot be created directly. It only defines the shared parts that subclasses complete.
Quick quiz
What does the private modifier do?
interface or type?
For object shapes they are nearly interchangeable. Differences worth knowing:
- Interfaces use
extends, types use&. - Interfaces can be reopened and added to later (declaration merging). Types cannot.
- Only
typecan name unions, tuples and other non-object shapes.
A practical rule: use interface for object shapes and for what classes implement, and type for unions and anything else. Pick one style for a project and stay consistent.
Try it yourself
Create a class Counter with a private count that starts at 0, an increment() method that adds 1, and a value() method that returns the count. The stub below will not compile until you fill it in.
Summary
extendsbuilds interfaces from other interfaces, and&combines types.- Classes can have
public,private,protectedandreadonlymembers. - Parameter properties declare and assign fields in the constructor.
implementschecks that a class matches an interface, andabstractclasses define shared parts for subclasses.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading