DEV Community

Cover image for Type vs Interface in TypeScript: What's the Difference & When to Use Each?
NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

7 2 2 2

Type vs Interface in TypeScript: What's the Difference & When to Use Each?

TypeScript offers two powerful tools for defining the shape of data: type and interface. But what’s the difference between them, and when should you use one over the other?

In this post, we'll cover everything you need to know about type vs interface, with examples, comparisons, and best practices.


🔹 What is a type in TypeScript?

A type is a TypeScript feature used to define aliases for primitive types, unions, intersections, tuples, and object shapes.

✅ Example:

type UserID = string | number;

type Point = {
  x: number;
  y: number;
};
Enter fullscreen mode Exit fullscreen mode

Types are flexible and work well for complex structures like mapped types, unions, and intersections.


🔸 What is an interface?

An interface is typically used to define the structure of an object. It supports declaration merging and is preferred for defining public APIs or class contracts.

✅ Example:

interface Person {
  name: string;
  age: number;
}
Enter fullscreen mode Exit fullscreen mode

You can extend interfaces to create complex structures.

interface Employee extends Person {
  role: string;
}
Enter fullscreen mode Exit fullscreen mode

🆚 Key Differences: type vs interface

Feature type interface
Object Shape Definition
Union/Intersection Types
Declaration Merging
Extending Types ✅ (via &) ✅ (via extends)
Use with Classes
React Props ✅ (often preferred)

🧠 When to Use type vs interface

Here are some common guidelines:

  • Use interface when:

    • You’re defining the shape of an object or class
    • You want to benefit from declaration merging
    • You're working with public APIs or React props
  • Use type when:

    • You need unions or intersections
    • You're defining primitive aliases or tuples
    • You're working with more complex type logic

💻 Real-World Example

// Using type
type User = {
  id: number;
  name: string;
};

type Admin = User & {
  role: 'admin';
};

// Using interface
interface IUser {
  id: number;
  name: string;
}

interface IAdmin extends IUser {
  role: 'admin';
}
Enter fullscreen mode Exit fullscreen mode

Both achieve similar results but may differ in behavior when extending or merging.


📽️ Watch the Full Video

Learn with visuals and live coding examples in this video:

👉 Watch now


🗨️ What Do You Prefer?

Do you use type, interface, or a mix of both in your projects? Let me know in the comments below or drop your questions!


🔖 Tags

TypeScript #WebDevelopment #Frontend #TypeVsInterface #JavaScriptToTypeScript #CodingTips #LearnToCode

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (6)

Collapse
 
nevodavid profile image
Nevo David

Perfect timing for this cuz I always end up googling it again and again lol

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Happy you found it helpful 😊

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Been switching back and forth on this forever, honestly - you think it actually matters much once your codebase gets big?

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Yes... I think so.

I usually use TYPE for simple declarations, but INTERFACE for more complex declarations

This may not have much impact but it keeps the codebase organised for me

Collapse
 
javierescartin profile image
Javier Escartin

Great examples to understand

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you 🙏

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay