Interfaces
An interface is a form with blanks to fill.
It lists the names and types an object must have.
You can reuse that form in many places.
Classes can also promise to match an interface.
Declare an interface
Use the word interface then a name.
Inside, list each blank and its type.
Objects that fill every blank match the form.
Optional blanks
Mark a blank with ? if it can be skipped.
Optional means the blank may be empty.
Still check before you use that value.
Readonly blanks
Readonly means fill once. Then leave it alone.
Good for ids and other sticky labels.
Extending interfaces
Extend means take one form and add more blanks.
The new form includes everything from the old one.
Plus the new blanks you add.
Implementing with a class
A class can say it implements an interface.
Implements means the class promises to fill the form.
TypeScript errors if a required blank is missing.
Declaration merging
If you write the same interface name twice, TypeScript merges them.
Merge means the blanks join into one bigger form.
That is useful for adding types to shared libraries.
Use an interface when you want a reusable form for object shapes.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.