TypeScript Aliases


TypeScript: Type Aliases

Type aliases let you create your own names for types — to make code easier to read and reuse.


What’s a Type Alias?

Think of a type alias like a shortcut.

Instead of writing the same type over and over, you give it a custom name.

It helps simplify long or complex type definitions.


How to Make One

Use the type keyword followed by your chosen name:

type ID = string | number; 

Now, instead of repeating string | number everywhere, just use ID:

let userId: ID = 101;
let postId: ID = "a1b2";

Both are valid because ID allows either a string or a number.


Useful with Objects Too

type Book = {   
    title: string;   
    pages: number;   
    isAvailable: boolean; 
}; 
let myBook: Book = {   
    title: "Clean Code",   
    pages: 464,   
    isAvailable: true 
}; 

Aliases in Functions

They can also define input or return types in functions:

type Status = "pending" | "success" | "failed";  

function updateStatus(status: Status) {   
    console.log("Status:", status);
 } 

Now the function only accepts those specific strings.


When to Use Type Aliases

  • When a type is long or repeated
  • For union or literal types
  • To make your code self-explanatory
  • To give structure to objects without using interfaces

Type Alias vs Interface

They can feel similar, but:

  • type can describe unions, primitives, functions, and tuples
  • interface is better for extending and organizing objects
Use what feels more readable — and remember, you can often choose either!

Prefer Learning by Watching?

Watch these YouTube tutorials to understand TYPESCRIPT Tutorial visually:

What You'll Learn:
  • 📌 TypeScript Tutorial #9 Type Aliases
  • 📌 Explained in 2 Minutes: Type vs Interface In Typescript
Previous Next