Module 28: Interfaces and Type Design

Lesson 3 of 5 · 15–25 minutes

Type aliases and when to use which

🏠The everyday picture

Two kinds of labels in an office: one kind for document folders (always describing a folder), and a general labelling machine that can name anything, from a folder to a colour code.

📘What it really is

type Paise = number;
type Status = "pending" | "paid" | "cancelled";
type Point = { x: number; y: number };
type PriceRule = (price: Paise) => Paise;

🖱️Try it

In types.ts, add type Paise = number;, type Status = ... and type PriceRule = (price: Paise) => Paise;. Update Product.price to use Paise, and type your makeDiscount function from Module 23 so that it returns a PriceRule.

✅Quick check

Can an interface describe a union like "S" | "M" | "L"?

Show the answer

No. Use a type alias for unions.

Does type Paise = number stop you passing a value in rupees?

Show the answer

No. It's just another name for number; it documents meaning only.

← PreviousOptional and readonly properties
ProLearners Assist