Intermediate · 1 of 16 · 12 min read
type versus interface
The two differences that are real — merging, and what each can name — and a rule for the cases where it genuinely does not matter.
- Type aliases
- Interfaces
For an object shape, type and interface do the same job. The question is asked so often
because the answer sounds like it should be a preference, and it is not: there are exactly two
differences that decide it, and everything else is noise.
What they share
interface UserInterface {
id: number;
name?: string;
readonly createdAt: Date;
greet(): string;
}
type UserType = {
id: number;
name?: string;
readonly createdAt: Date;
greet(): string;
};
const a: UserInterface = { id: 1, createdAt: new Date(0), greet: () => "hi" };
const b: UserType = a; // structurally identical
console.log(b.greet());
Optional members, readonly, methods, index signatures, generics, extension and implements —
all of it works both ways. Neither is “the modern one”.
Difference one: what can be named
An interface describes an object-like shape. An alias names any type:
type ID = string | number; // union
type Point = [number, number]; // tuple
type Name = string; // primitive alias
type Callback = (data: string) => void;
type Keys = keyof { a: 1; b: 2 }; // computed
type Nullable<T> = { [K in keyof T]: T[K] | null }; // mapped
type Unwrap<T> = T extends Promise<infer V> ? V : T; // conditional
const id: ID = 1;
const p: Point = [0, 0];
console.log(id, p);
None of those are expressible as an interface. Since any non-trivial codebase needs unions and mapped types, you will be writing aliases regardless — the question is only what to use for the plain shapes.
Difference two: merging
interface Animal { name: string }
interface Animal { age: number }
const rex: Animal = { name: "Rex", age: 3 }; // both required
type Pet = { name: string };
// type Pet = { age: number }; // Duplicate identifier 'Pet'
type Dog = Pet & { age: number };
console.log(rex.age);
Merging combines members that agree; a conflicting property is an error, while a repeated method becomes an overload:
interface Config { port: number }
// interface Config { port: string } // Subsequent declarations must have the same type
interface Api { get(path: string): string }
interface Api { get(path: string, retries: number): string } // an overload
declare const api: Api;
console.log(typeof api.get);
This is the behaviour that decides the choice for a library. A package that publishes interfaces lets consumers augment its types; one that publishes aliases does not.
// A consumer adding to a type they do not own:
declare global {
interface Window { myApp?: { version: string } }
}
export {};
And it is the behaviour an application usually does not want: a second declaration of the same name is a mistake, and an alias turns it into an error.
extends versus &
Both combine shapes. They differ in where a conflict surfaces:
interface Base { id: number }
// interface Bad extends Base { id: string }
// Interface 'Bad' incorrectly extends 'Base' — reported at the declaration
type Alias = Base & { id: string }; // no complaint here...
declare const value: Alias;
console.log(typeof value.id);
// ...but `id` is `never`, so nothing can be assigned:
// const x: Alias = { id: 1 }; // not assignable
// const y: Alias = { id: "1" }; // not assignable either
extends checks as it goes; & combines and lets the impossibility show up wherever someone
tries to build the value. There is also a small performance difference — the compiler caches
interface relationships and recomputes large intersections — which only matters on big codebases
but matters a lot there.
type |
interface |
|
|---|---|---|
| Object shapes | yes | yes |
| Unions, tuples, primitives | yes | no |
| Mapped / conditional types | yes | no |
| Declared twice | error | merges |
| Combining | &, unchecked |
extends, checked |
| Implemented by a class | yes | yes |
| Hover and error messages | often inlined in full | usually shows the name |
| Compiler caching | recomputed | cached |
A working rule
- Union, tuple, primitive alias, or anything computed →
type. There is no alternative. - A shape published for others to extend — a library’s props, a plugin contract →
interface. - A plain shape inside an application → either. Pick one for consistency; most teams use
interfacefor shapes andtypefor the rest.
// Both correct, both identical:
interface PropsA { title: string; onClose: () => void }
type PropsB = { title: string; onClose: () => void };
const a: PropsA = { title: "x", onClose: () => {} };
const b: PropsB = a;
console.log(b.title);
Both work with classes
A common misconception is that implements needs an interface:
type Serialisable = { toJSON(): string };
interface Printable { print(): void }
class Report implements Serialisable, Printable {
toJSON() { return "{}"; }
print() { console.log("printing"); }
}
new Report().print();
The restriction is on shape, not on the keyword: a class can implement anything object-like, which rules out a union.
type Either = { a: string } | { b: string };
// class Impossible implements Either {} // A class can only implement an object type
Common mistakes
Treating it as a style question
Mistake. A team standard of “always type” or “always interface”.
Why it fails. Half the time the choice is forced — unions need an alias, augmentation needs
an interface — so the rule gets broken and the consistency it was for disappears anyway.
What to do instead. Let the two differences decide, and only fall back to a convention for
plain shapes.
Publishing aliases from a library
Mistake. export type Options = { ... } in a package consumers may need to extend.
Why it fails. They cannot augment it. Their only options are to fork the type or to cast.
What to do instead. export interface Options, so module augmentation is available.
Building a hierarchy with &
Mistake. type Admin = User & { role: string } across several levels.
Why it fails. Conflicts become never silently, errors read as long inlined intersections,
and the compiler recomputes the whole thing each time.
What to do instead. interface Admin extends User, which checks the conflict at the
declaration and reads better in every error message.
Check yourself
2 questions from the bank on what you just read. Answering here does not affect your quiz history.
What does extends give you that an intersection & does not?
Merging only combines members that agree; a conflict is reported rather than resolved.
Why does the second declaration fail?
interface Config {
port: number;
}
interface Config {
port: string;
}Further reading
Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.
Reaching the end of a lecture marks it as read.