DEV Community

Cover image for TypeScript: Do you know the differences between type and interface?
Ravi Anand
Ravi Anand

Posted on

TypeScript: Do you know the differences between type and interface?

They look almost identical, but there are some important differences. Let's explore when to use each.

When defining the shape of an object in TypeScript, we generally have two options: type and interface.

Let's start with a simple example:

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

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

Both approaches describe an object with two properties: name and age.

A quick note: You might see developers using prefixes such as I for interfaces and T for type aliases. These are personal naming conventions, not TypeScript requirements. Generally, avoid unnecessary prefixes.

Another option is class, but unlike type and interface, a class is a JavaScript runtime construct that introduces both a value and a type.

1. What happens when we introduce an additional property?

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

const person: Person = {
  name: 'Alex',
  age: 20,
  height: 175,
};
Enter fullscreen mode Exit fullscreen mode

TypeScript error

TypeScript complains because height isn't part of the defined object shape.

The same behaviour applies to interfaces.

This is known as an excess property check. Notice that there is no difference between type and interface here.

2. Can they extend each other?

Interestingly, an interface can extend a type alias, and a type alias can extend an interface using intersection types.

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

interface Employee extends Person {
  salary: number;
}
Enter fullscreen mode Exit fullscreen mode

Alternatively:

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

type Employee = Person & {
  salary: number;
};
Enter fullscreen mode Exit fullscreen mode

Both approaches produce an equivalent object shape.

However, there's one important caveat: an interface can only extend an object type or an intersection of object types with statically known members.

For example:

type CustomSalary = string | number;

interface Candidate extends CustomSalary {
  // Error!
}
Enter fullscreen mode Exit fullscreen mode

An interface cannot extend a union type.

3. Union types: An advantage of type

We've already seen one important distinction: we have union types, but no equivalent union interface syntax.

type StringOrNumber = string | number;
Enter fullscreen mode Exit fullscreen mode

A variable of this type can hold either a string or a number.

Type aliases also allow us to combine unions and intersections:

type Input = {
  value: string;
};

type Output = {
  result: number;
};

type InputOrOutput = (Input | Output) & {
  name: string;
};
Enter fullscreen mode Exit fullscreen mode

This represents either an Input or an Output, with an additional required name property.

Type aliases are particularly flexible because they can represent unions, intersections, primitives, tuples, mapped types and conditional types.

4. Declaration merging: An advantage of interface

On the other hand, interfaces have a capability that ordinary type aliases don't: declaration merging.

Consider this example:

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

interface Person {
  city: string;
}
Enter fullscreen mode Exit fullscreen mode

Surprisingly, TypeScript doesn't throw an error!

Instead, it merges both declarations into a single interface.

const person: Person = {
  name: 'Alex',
  age: 25,
  city: 'Berlin',
};
Enter fullscreen mode Exit fullscreen mode

Our Person interface now contains all three properties.

This behaviour is known as declaration merging.

It's particularly useful when extending third-party library declarations, augmenting global interfaces, or designing extensible public APIs.

However, it can also introduce unexpected side effects if used unintentionally.

Type aliases don't support this behaviour:

type Person = {
  name: string;
};

// Error: Duplicate identifier 'Person'.
type Person = {
  age: number;
};
Enter fullscreen mode Exit fullscreen mode

5. So, which one should you use?

The distinction between type and interface has become increasingly blurred over the years. In many situations, either option works perfectly well.

My recommendation is to consider two things: consistency and augmentation.

For existing projects:

  • Does your codebase consistently use interfaces? Stick with interfaces.
  • Does it primarily use type aliases? Follow that convention.

For new projects:

Consider whether declaration merging is something you need.

  • Use interface when defining object contracts, particularly when you want library consumers to augment your public declarations.
  • Use type when you need unions, intersections, primitives, tuples or more advanced type compositions.

Personally, I tend to prefer type for internal application types unless I have a specific reason to use an interface.

That's a personal preference, not a universal TypeScript rule.

Final thoughts

Both type and interface are powerful ways to describe types in TypeScript.

Understand their differences, follow your project's conventions, and choose the one that best fits your requirements.

The goal isn't to use one everywhere. It's to write TypeScript that is predictable, maintainable and easy for other developers to understand.

What about you?

Do you prefer type or interface in your projects? Is there a particular reason behind your choice?

I'd love to hear your thoughts!

TypeScript #JavaScript #Angular #React #Frontend #FrontendDevelopment #WebDevelopment #SoftwareEngineering

Top comments (0)