Careeroza
Type Aliases
Give a Type a Name
Writing the same object type again and again is tiring. A type alias gives it a name with the type keyword. By convention the name is PascalCase.
Example
This is the start of our ShopEase e-commerce model used throughout the course.
type Category = "Electronics" | "Books" | "Fashion";
type Product = {
id: number;
name: string;
description: string;
price: number;
stock: number;
category: Category;
};
const laptop: Product = {
id: 1,
name: "ShopEase Laptop 14",
description: "Thin and light",
price: 54999,
stock: 12,
category: "Electronics",
};
function isAvailable(p: Product): boolean {
return p.stock > 0;
}
console.log(laptop.name, isAvailable(laptop));Output
ShopEase Laptop 14 true
Aliases for Any Type
Example
An alias can name any type, not just objects: primitives, unions, tuples, functions.
type ProductId = number;
type Price = number;
type Point = [x: number, y: number];
type Formatter = (amount: Price) => string;
const id: ProductId = 42;
const origin: Point = [0, 0];
const inr: Formatter = (amount) => "Rs. " + amount.toLocaleString("en-IN");
console.log(id, origin, inr(1250000));Output
42 [ 0, 0 ] Rs. 12,50,000
Note: An alias does not create a new type. ProductId and number are interchangeable. (Branded types in the advanced level fix that.)
Combining Types with &
Example
An intersection A & B has all properties of both.
type Timestamps = { createdAt: string; updatedAt: string };
type User = { id: number; name: string; email: string };
type UserRecord = User & Timestamps;
const u: UserRecord = {
id: 7, name: "Arjun", email: "[email protected]",
createdAt: "2026-01-15T10:00:00Z", updatedAt: "2026-02-01T09:30:00Z",
};
console.log(Object.keys(u).join(", "));Output
id, name, email, createdAt, updatedAt