4️⃣ 🧱 TypeScript Collections & Custom Types – Arrays, Tuples, Enums & More (2025)
🧲 Introduction – Why Learn TypeScript Collections & Custom Types?
When building scalable applications, developers need more than just primitive types. Collections and custom types in TypeScript—such as arrays, tuples, enums, and type aliases—provide powerful ways to model real-world data with accuracy and flexibility. These constructs let you represent structured, ordered, or variant data with clear intent and maintainable code.
🎯 In this guide, you’ll learn:
- How to declare and work with TypeScript arrays and tuples
- When to use enums for named constants
- How to define structured object types
- Creating reusable and union types using type aliases
📘 Topics Covered
🧩 Topic | 📌 Description |
---|---|
TS Arrays | Define and manipulate lists of values using typed arrays |
TS Tuples | Represent fixed-length arrays with known, ordered types |
TS Enums | Use named constants for better code readability |
TS Object Types | Create structured custom object shapes with required/optional properties |
TS Type Aliases | Define reusable custom types for clarity and DRY code |
TS Union Types | Allow variables to hold values of multiple possible types |
📂 TypeScript Arrays – Typed Collections
let numbers: number[] = [1, 2, 3];
let fruits: Array<string> = ["apple", "banana", "cherry"];
✅ Both type[]
and Array<type>
are valid.
📦 TypeScript Tuples – Fixed-Size, Ordered Types
let user: [string, number];
user = ["Alice", 25]; // ✅
🔐 Tuples enforce order and type at each index, perfect for structured pairs.
🎯 TypeScript Enums – Define Named Constants
enum Direction {
Up,
Down,
Left,
Right,
}
let move: Direction = Direction.Left;
Enums can also have custom string or number values.
🏗️ TypeScript Object Types – Custom Structures
let employee: { name: string; age?: number } = {
name: "John",
};
🧱 Use ?
to mark optional properties.
🧾 Type Aliases – Reusable Type Definitions
type UserID = string | number;
let id: UserID = 101;
Aliases simplify and centralize complex or repeated types.
🔗 Union Types – Multi-Form Variables
let result: string | number;
result = "pass";
result = 95;
Unions are essential for handling multiple valid data formats.
📌 Summary – Recap & Next Steps
With collections like arrays and tuples and custom types like aliases and unions, TypeScript gives you tools to represent data with precision and flexibility. These are foundational in crafting scalable applications with predictable behavior.
🔍 Key Takeaways:
- Arrays store lists of typed elements
- Tuples represent fixed structures with positional typing
- Enums enhance code clarity with named constants
- Use aliases and unions for reusable, flexible type modeling
⚙️ Real-World Relevance:
Collections and custom types are essential in real apps where you deal with structured data, optional properties, and varying return types from APIs.
❓ FAQ – Collections & Custom Types in TypeScript
❓ What is the difference between an array and a tuple in TypeScript?
✅ Arrays hold variable-length, same-type items; tuples have fixed-length, positional types.
❓ When should I use enums in TypeScript?
✅ Use enums when you need a set of named constants that improve code readability and reduce errors.
❓ What is a type alias in TypeScript?
✅ A type alias creates a new name for a type or a union of types, making complex types more readable and reusable.
❓ How do union types help in real-world applications?
✅ Union types allow a variable to accept multiple types, making them ideal for flexible function parameters or API responses.
Share Now :