TypeScript · Lesson 46 of 120
Implementing Interfaces
A contract.
An interface can be a contract for classes. A class that implements it promises to have every property and method the interface lists. TypeScript checks that promise.

- A contract
- implements
- Checked
A broken promise
Every shipper must have a name, and a quote method. The courier class implements shipper, but forgets the quote method. Run it. TypeScript reports the missing method.
app.tstypescript
interface Shipper {
name: string;
quote(weightKg: number): number;
}
class Courier implements Shipper {
name = "FastBikes Courier";
}Output
app.ts line 6: Class 'Courier' incorrectly implements interface 'Shipper'. Property 'quote' is missing in type 'Courier' but required in type 'Shipper'.
Two shippers
Now both classes implement the full contract. A function can take any shipper, and doesn't care which class it is. Run it.
app.tstypescript
interface Shipper {
name: string;
quote(weightKg: number): number;
}
class Courier implements Shipper {
name = "Courier";
quote(kg: number) { return 5 + kg * 0.5; }
}
class Post implements Shipper {
name = "Post";
quote(kg: number) { return kg < 10 ? 4.99 : 12.99; }
}
const shippers: Shipper[] = [new Courier(), new Post()];
for (const s of shippers) console.log(s.name, s.quote(14));Output
Courier 12 Post 12.99
Summary
Implements checks that a class keeps the interface's promises. Next, inheritance.
Key takeaways- implements Shipper
- Missing methods caught
- Any Shipper works
Next up: Inheritance — extends