Module 28: Interfaces and Type Design

Lesson 1 of 5 · 15–25 minutes

Interfaces

🏠The everyday picture

A standard admission form that every school in a district uses. Once the form is agreed, everyone knows exactly which details a student record has.

📘What it really is

interface Product {
  id: string;
  name: string;
  category: Category;
  price: number;   // in paise
  stock: number;
}
function describe(product: Product): string { ... }
const products: Product[] = [ ... ];
interface DigitalProduct extends Product {
  downloadUrl: string;
}

🖱️Try it

Create types.ts. Write interfaces for Product, Customer (id, name, phone, city) and OrderLine (productId, quantity). Use Product[] for your products array in app.ts, and deliberately leave out a property from one product to see the error.

✅Quick check

What happens if a product object is missing a property its interface requires?

Show the answer

TypeScript reports an error, before the code runs.

Why write // in paise above price?

Show the answer

It records the unit, and appears when hovering, preventing someone treating it as rupees.

← BackModule 28
ProLearners Assist