Should we use type or interface in typescript
Similarities
- Both Type Aliases and interface can be used to describe object or function types
type Point = {
x: number;
y: number;
};
type SetPoint = (x: number, y: number) => void;
// -----------------------------------
interface Point {
x: number;
y: number;
}
interface SetPoint {
(x: number, y: number): void;
}- Both type aliases and interface can be extended
- Type aliases are extended by
&, - while interfaces are extended by
extends. → An interface extend the type defined by the type alias through extends => Vice versa
- Type aliases are extended by
type PointX = {
x: number;
};
interface PointY {
y: number;
}
type line = PointX &
PointY & {
length: number;
};
interface triangle extends PointX, PointY {
z: number;
perimeter: number;
stretch: number;
}Differences
- Type aliases can define aliases for primitive types, union types, or tuple types, while interfaces cannot
type MyNumber = number; // primitive type
type StringOrNumber = string | number; // union type
type Point = [number, number]; // tuple type- Interfaces with the same name are automatically merged(Declaration Merging) while type aliases are not:
interface User {
name: string
}
interface User {
age: number
}
//👌: it can work currectly
const user:User {
name: "yushaku",
age: 23
}type User = {
name: string;
};
//🐞 get error heae when have duplicate type
type User = {
age: number;
};Using the feature of declaration merging, we can provide users with better security when developing third-party libraries. For example, the webext-bridge library uses interface to define the ProtocolMap interface, so that users can freely extend the ProtocolMap interface. After that, when using the onMessage function provided inside the library to monitor custom messages, we can infer the message body types corresponding to different messages.
extends ProtocolMap interface
import { ProtocolWithReturn } from "webext-bridge";
declare module "webext-bridge" {
export interface ProtocolMap {
foo: { title: string };
bar: ProtocolWithReturn<CustomDataType, CustomReturnType>;
}
}listen for custom messages
import { onMessage } from 'webext-bridge'
onMessage('foo', ({ data }) => {
// type of `data` will be `{ title: string }`
console.log(data.title)
}Use Case
When to use type
- When defining aliases for primitive types, use
type - When defining a tuple type, use
type - When defining a function type, use
type - When defining union types, use
type - When defining the mapped types, use
type
When to use interface
my recommendation is to consistently use type in most cases and only use interface when either of the following is true:
- You want to take advantage of the "merging" feature of
interface. - You have a style code involving class/interface hierarchies.
In typescript, type and interface are very similar constructs when used for typing objects. Though maybe controversial,
Related Posts
Find more posts like this one.

January 10, 2024
I'm Done Typing npm
Are you tired of typing npm?
Read more
May 29, 2025
Load balancer RPC endpoints
Did your Dapp cash because of RPC endpoint?
Read more
May 15, 2025
Solidity: Storage Slots of Complex Types
This article explains how Solidity stores smart contract data using storage slots, packing for efficiency, and Yul assembly for direct storage access
Read more
May 13, 2025
Solidity: Storage Slots of Primary Types
This article explains how Solidity stores smart contract data using storage slots, packing for efficiency, and Yul assembly for direct storage access
Read more
May 13, 2025
Cache Strategies
Cache strategies are a way to improve the performance of a system.
Read more
May 9, 2025
Load Balancer
A load balancer is a device that distributes network traffic between multiple servers
Read more
May 9, 2025
Rate Limiting
Rate limiting is a technique used to control the rate of requests to a service.
Read more
May 13, 2025
Redis
Redis is an open-source, in-memory data structure store used as a database, cache, and message broker.
Read more
May 19, 2025
Javascript: deep cloning object methods
Read more
May 7, 2025
Prettier merged type
Prettier merged type
Read more