TypeScript · Lesson 20 of 42
Interfaces and how they differ from type aliases
Describe object shapes with interface, extend interfaces, merge declarations, and learn when to choose an interface and when a type alias.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will know the two main ways to name an object shape, interface and type, what each one can do that the other cannot, and how to choose without agonising. You will also see declaration merging, a feature unique to interfaces that you will meet when working with libraries.
Declaring an interface
An interface gives a name to an object shape:
interface User {
id: number;
name: string;
email?: string;
readonly createdAt: string;
}
const user: User = { id: 1, name: "Asha", createdAt: "2026-11-24" };
console.log(user.name);AshaEverything you learned about object types applies: optional properties with ?, readonly properties, and the excess property check. A method can be described in two styles, which are the same for most purposes:
interface Greeter {
greet(name: string): string;
shout: (name: string) => string;
}
const greeter: Greeter = {
greet: name => `Hello, ${name}`,
shout: name => `HELLO, ${name.toUpperCase()}`,
};
console.log(greeter.greet("Asha"), greeter.shout("Ben"));Hello, Asha HELLO, BENExtending interfaces
Interfaces can build on each other with extends:
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
const dog: Dog = { name: "Rex", breed: "Beagle" };
console.log(dog.name, dog.breed);Rex BeagleAn interface can extend several others (interface C extends A, B). If the new interface changes an inherited property to an incompatible type, the compiler stops you right away:
interface Base {
id: string;
}
interface Derived extends Base {
id: number;
}example.ts(5,11): error TS2430: Interface 'Derived' incorrectly extends interface 'Base'.
Types of property 'id' are incompatible.
Type 'number' is not assignable to type 'string'.This is a strength of extends compared with the intersection operator from the last lesson, where a conflict only silently turns into never.
Declaration merging
Here is a feature only interfaces have. Declare two interfaces with the same name in the same scope and TypeScript merges them into one:
interface Settings {
theme: string;
}
interface Settings {
fontSize: number;
}
const settings: Settings = { theme: "dark", fontSize: 14 };
console.log(settings);{ theme: 'dark', fontSize: 14 }A type alias cannot do this: declaring the same alias twice is an error. Merging looks like a curiosity, but it matters in practice. Libraries and the language itself use it so you can add to an existing type, for example adding a property to a global type or to a library's interface. For code you own, it is mostly a surprise to avoid: accidentally reusing a name merges instead of erroring.
Interface versus type
Both describe object shapes, and for objects they are nearly interchangeable. The differences:
interface |
type |
|
|---|---|---|
| Object shapes | yes | yes |
| Extend another | extends |
& intersection |
| Declaration merging | yes | no, error |
| Unions, tuples, primitives | no | yes |
| A class can implement it | yes | yes, if it is an object shape |
Only a type alias can name a union or a tuple:
type Id = string | number;
type Pair = [string, number];
const id: Id = "u-1";
const pair: Pair = ["score", 10];
console.log(id, pair);u-1 [ 'score', 10 ]How to choose
You will see strong opinions online, but a calm summary is enough:
- If you need a union, a tuple or a name for a primitive, use
type. You have no choice. - For ordinary object shapes, either works. Many teams use
interfacefor objects and public APIs andtypefor everything else; others usetypeeverywhere. Both are fine. - Pick one rule for the project and apply it. A consistent codebase matters more than the particular choice.
This course mostly uses type for short examples and interface where a shape is meant to be extended or implemented by a class.
Interfaces and classes
Classes can promise to follow an interface with implements. The compiler then checks that the class really has what the interface requires. The lesson on abstract classes shows this in detail. For now, remember that it is one reason interfaces are popular for describing what something can do, apart from how it does it.
Common mistakes
Merging by accident. Reusing an interface name in the same scope merges the declarations instead of reporting an error.
Trying to name a union with interface. Use type.
Switching between interface and type randomly. Pick a convention.
Expecting interfaces to exist at run time. They are erased, like all types. You cannot check x instanceof User.
Exercise
Define interface Employee { name: string; salary: number } and interface Manager extends Employee { reports: Employee[] }. Create a manager with two reports and print their names. Then try making Manager redefine salary as a string and read the message. Finally, merge a second Employee declaration that adds a department field and observe that existing objects now need it.
Next: Enums and their alternatives.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.