DEV Community

Cover image for 🚀 Type Alias in TypeScript: Why I Use Them Every Day (And How They Compare to Interfaces)
mashayeakh islam
mashayeakh islam

Posted on

🚀 Type Alias in TypeScript: Why I Use Them Every Day (And How They Compare to Interfaces)

If you’ve already dipped your toes into TypeScript, you’ve probably met that one feature everyone talks about but no one fully explains at the start.

🧩 What Is a Type Alias?

A type alias is basically giving a nickname to a type.
Instead of rewriting the same structure again and again, you wrap it inside a type and reuse it.

A basic explanation?

Think of it like giving your WiFi a name.
The internet was always there, but naming it “DragonSlayer-5G” makes life easier.

Here’s a simple example from my playground:

// type alias
type USER = {
  name: string;
  contactNo: string;
  address: {
    division: string;
    city: string;
  };
  gender: "male" | "female";
};

const user1: USER = {
  name: "Mashayeakh",
  contactNo: "01777",
  address: {
    division: "Dhaka",
    city: "Dhaka"
  },
  gender: "male"
};

console.log(user1);

Enter fullscreen mode Exit fullscreen mode

😄 Why I Like Type Aliases

When I'm structuring data models, a type alias feels natural. It’s straightforward and doesn’t pretend to be anything else. It just bundles types together and says, “Bro, use me wherever you want.”

You can even reuse it across multiple variables:

const user2: USER = {
  name: "Emma",
  contactNo: "017988",
  address: {
    division: "UK",
    city: "UK"
  },
  gender: "female"
};

console.log(user2);
Enter fullscreen mode Exit fullscreen mode

This alone saves you from rewriting properties all day.

🍕 Type Alias for Functions (Tasty Feature)

Types aren’t only for objects. You can name a function’s signature as well.

type AddFunc = (num1: number, num2: number) => number;

const add: AddFunc = (num1, num2) => {
  return num1 + num2;
};

console.log(add(2, 5));

Enter fullscreen mode Exit fullscreen mode

_ This makes your functions consistent and self-documented.
It also prevents “creative” arguments like strings, booleans, or emotions entering your math functions._

⚔️ Intersection Types (Combining Multiple Types)

Sometimes you want to combine different structures.
Type alias handles that with the & operator.

Like merging two Marvel characters but without copyright issues.

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

type Role = {
  role: "admin" | "user";
};

type UserWithRole = User & Role;

const user1: UserWithRole = {
  name: "MR.X",
  age: 100,
  role: "admin"
};

console.log(user1);
Enter fullscreen mode Exit fullscreen mode

Here, UserWithRolebecomes a new super-type that contains everything.

🆚 Type vs Interface — The Battle That Never Ends

If you Google “type vs interface TypeScript”, you’ll find 500 different answers.
Some swear by interface. Others defend typelike it's their first-born child.

Let me explain it cleanly.

✔ Interfaces are designed specifically for object shapes
✔ Types are more flexible — they work with primitives, unions, tuples, functions, and objects too

Both can describe objects, but types can do much more.

Let’s compare both with the same example.

🧱 Using Interface

interface IUser {
  name: string;
  age: number;
}

interface IUserWithRole extends IUser {
  role: "admin" | "user";
}

const user3: IUserWithRole = {
  name: "MIP",
  age: 123,
  role: "user"
};

console.log(user3);

Enter fullscreen mode Exit fullscreen mode

Interfaces shine when you need extension and structured modeling.
They support declaration merging, are extendable, and feel very OOP-friendly.

🔗 Using Type Alias

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

type Role = {
  role: "admin" | "user";
};

type UserWithRole = User & Role;

Enter fullscreen mode Exit fullscreen mode

Types don’t use extends, but the & operator does the same job.
The difference is more about preference and convenience.

🥊 So When Should You Use What?

Use type when:

  • You need unions (string | number)
  • You need intersections
  • You define primitives (type ID = string | number)
  • You define function signatures
  • You want one flexible tool for everything

Use interface when:

  • You are modeling objects heavily
  • You want declaration merging
  • You prefer OOP-style extensions
  • You want a consistent structure across your app

Both work. Both are valid.
TypeScript doesn't judge you. Your team might, though.

😄 A Tiny Joke Before You Go

TypeScript developers be like:

“I don’t have trust issues. I just don’t trust any.”

🎯 Final Thoughts

If you’re learning TypeScript, mastering typeand interfaceearly will make your life easier.
These features help you write safer code and help your future teammates avoid headaches.

This post is part of my TypeScript learning journey, and I hope it helps someone who’s just getting started.

If you're a beginner, keep exploring. If you're experienced, feel free to roast my examples in the comments.

Happy coding!

Written by Masayeakh full stack dev sharing his TypeScript journey.

Top comments (0)