Interface vs Type in TypeScript: What’s The Deal?
In today’s post, we’re going to demystify a topic that often stirs up confusion among TypeScript developers: the difference between type and interface. As these two constructs share many similarities in TypeScript, it’s not uncommon to find yourself pondering which one to use.
Let’s get right into it!
What are type
and interface
?
Before delving into the differences, let’s first understand what they are.
- Type: The
type
keyword in TypeScript is used for type annotations and type aliasing. It lets you create complex types from primitive ones and also enables creating unions, intersections, mapped types, conditional types, and more.
type Point = {
x: number;
y: number;
};
At first glance, type
and interface
may look like they’re doing the same thing. In fact, in many use cases, they’re interchangeable. But let’s dig into where they differ.
Differences between type
and interface
Extensibility
One fundamental difference is the way type
and interface
handle extensibility.
Interfaces are extendable and mergeable. It means that you can create an interface, and later extend or merge it with new properties.
interface Point {
x: number;
}
interface Point {
y: number;
}
let point: Point = {
x: 1,
y: 2,
};
Types, on the other hand, do not support merging in the same way. Once a type alias is created, it’s fixed – you can’t alter it or add new properties. You can, however, create a new type that extends the previous one.
type Point = {
x: number;
};
type PointWithY = Point & { y: number; };
let point: PointWithY = {
x: 1,
y: 2,
};
Type Alias vs Interface
A crucial difference lies in how TypeScript treats type
and interface
. Types are more powerful and flexible because they can represent any valid type: primitives, union/intersection types, tuples, etc.
type StringOrNumber = string | number;
type Coordinate = [number, number];
Interfaces, in contrast, are more restricted but can be more readable and are generally preferred when defining the shape of object-like structures. They also provide better editor support for auto-completion.
Representation
A Type, once declared, cannot be re-declared. However, Interfaces are open-ended and can be declared multiple times, with the declarations merging.
When to use type
or interface
?
Now that we have established the differences between type
and interface
, the million-dollar question remains – when should we use which?
Here are a few suggestions:
- When defining the shape of an object or when creating a contract for classes, use
interface
. Interfaces provide a cleaner syntax, and are easier to extend, merge and implement. - When you need to use union or intersection types, tuples, mapped types, or conditional types or you are defining non-object data structures, use
type
.
To wrap things up, both type
and interface
are powerful tools in TypeScript’s toolbox. The differences between them are subtle, and the choice often comes down to personal preference and the specific needs of your project.
Remember that TypeScript aims to enhance JavaScript while keeping the learning curve and cognitive load minimal. Therefore, understanding how to wield these tools effectively is a step in the right direction to create type-safe JavaScript.