TypeScript Type Aliases

TypeScript tutorial

🏷️ TypeScript Type Aliases

Type Aliases in TypeScript allow you to give a name to a type.

They make your code cleaner, reusable, and easier to understand, especially for complex types.


1️⃣ Basic Type Alias

Instead of repeating a type again and again, define it once.

type ID = number;

let userId: ID = 101;
let orderId: ID = 202;


2️⃣ Type Alias for Object Types

Very common and very useful.

type User = {
id: number;
name: string;
age: number;
};

let admin: User = {
id: 1,
name: "Sanjit",
age: 25
};

✅ Cleaner
✅ Reusable
✅ Readable


3️⃣ Optional & Readonly in Type Aliases

type Product = {
readonly id: number;
name: string;
price?: number;
};

let item: Product = {
id: 101,
name: "Laptop"
};


4️⃣ Type Alias with Union Types

Combine multiple possible types.

type Status = "pending" | "approved" | "rejected";

let orderStatus: Status = "approved";

❌ Error:

orderStatus = "done";

5️⃣ Type Alias for Function Types

Define the function structure.

type Add = (a: number, b: number) => number;

let sum: Add = (x, y) => x + y;


6️⃣ Type Alias for Arrays

type StringArray = string[];

let names: StringArray = ["Ram", "Shyam"];


7️⃣ Type Alias for Tuples

type UserTuple = [number, string];

let user: UserTuple = [1, "Sanjit"];


8️⃣ Type Alias with Intersection Types

Combine multiple types into one.

type Person = {
name: string;
};

type Employee = {
empId: number;
};

type Staff = Person & Employee;

let staffMember: Staff = {
name: "Amit",
empId: 1001
};


9️⃣ Type Alias vs Interface

Feature Type Alias Interface
Object Types ✅ Yes ✅ Yes
Union Types ✅ Yes ❌ No
Intersection Types ✅ Yes ❌ No
Extend / Merge ❌ No ✅ Yes
Preferred for Complex types Object shapes

📌 Rule of thumb:

  • Use type for unions, primitives, functions

  • Use interface for object structures (especially public APIs)


🔑 Type Aliases Summary

Use Case Example
Primitive type ID = number
Object type User = {}
Union `type Status = “a”
Function type Fn = () => void
Array type List = string[]
Tuple type T = [number, string]
Intersection type A & B

⭐ Best Practices

✔ Use type aliases to avoid repetition
✔ Use meaningful names
✔ Prefer type aliases for unions & functions
✔ Keep types small and composable

You may also like...