Level 6
Interfaces
Define reusable, named object shapes with interfaces. Learn extension, method signatures, structural typing in depth, and declaration merging - plus when to reach for an interface over an inline type.
20 min read + quiz
From Inline Types to Interfaces
In the previous lesson you wrote object types inline, which works but does not scale: the same shape repeated across five functions is five places to update when it changes. An interface gives a shape a name, defined once and referenced everywhere. Beyond reuse, names carry meaning: a parameter of type User tells a reader far more than an anonymous blob of properties.
Interface syntax looks like an object type with a name in front. Everything you learned about object types applies: optional properties with ?, readonly modifiers, nested shapes, and index signatures all work identically inside an interface.
Interfaces exist only at compile time, like all TypeScript types. They are erased from the output, so an interface costs nothing at runtime; it is pure documentation that the compiler enforces.
interface User {
readonly id: number;
name: string;
email: string;
nickname?: string;
}
function describeUser(user: User): string {
const display = user.nickname ?? user.name;
return "#" + user.id + " " + display + " <" + user.email + ">";
}
const ada: User = { id: 1, name: "Ada Lovelace", email: "ada@calc.io" };
console.log(describeUser(ada));A named shape, defined once, reused by every function that works with users.
Methods and Function Properties
Interfaces can require behavior as well as data. A method signature like 'area(): number' says every implementer must provide an area method returning a number. You can write this in method shorthand ('area(): number') or as a function-typed property ('area: () => number'); the two are nearly interchangeable for everyday use.
This lets interfaces describe capabilities, not just records. An interface like Serializer with a 'serialize(value: unknown): string' method describes anything that can serialize, whether that is a class instance, an object literal, or something returned from a factory function.
Because TypeScript is structural, an object literal with the right method shape satisfies the interface automatically; there is no 'implements' keyword needed outside of classes. Any value with matching members is accepted, wherever it came from.
interface Shape {
name: string;
area(): number;
}
const circle: Shape = {
name: "circle",
area() {
return Math.PI * 5 * 5;
},
};
const square: Shape = {
name: "square",
area: () => 4 * 4,
};
console.log(circle.name, circle.area().toFixed(2)); // circle 78.54
console.log(square.name, square.area()); // square 16Interfaces describe capabilities: any object with a matching area method qualifies as a Shape.
Extending Interfaces
Interfaces compose through extension. 'interface Admin extends User' creates a new interface with everything from User plus whatever Admin adds. This models 'is-a' relationships cleanly: every Admin is a User with extra powers, and any function that accepts a User will happily accept an Admin.
An interface can extend multiple parents at once, merging all their members. This is a lightweight form of composition that classes (which can extend only one base class) cannot match. Use it to assemble complex shapes from small, focused pieces: Timestamped, Identified, Named, and so on.
Extension beats duplication for maintainability. If User gains a new required property, every extending interface automatically requires it too, and the compiler points out each place that needs updating. Copy-pasted shapes drift apart silently.
interface Identified {
readonly id: number;
}
interface Timestamped {
createdAt: string;
}
interface Post extends Identified, Timestamped {
title: string;
body: string;
}
const post: Post = {
id: 7,
createdAt: "2026-07-11",
title: "Interfaces rock",
body: "Compose small shapes into big ones.",
};
console.log(post.id, post.title);Multiple extension assembles a shape from small, single-purpose interfaces.
Structural Typing, Revisited
TypeScript never asks whether a value was declared as a particular interface, only whether its structure matches. Two interfaces with identical members are fully interchangeable, and an object created with no interface in sight satisfies any interface it happens to match. This is often summarized as duck typing: if it walks like a duck and quacks like a duck, it is a duck.
This differs sharply from nominally-typed languages like Java, where a class must explicitly declare 'implements Duck' to count. Structural typing suits JavaScript's culture of plain object literals and makes interfaces easy to adopt incrementally, since existing objects that already have the right shape need no changes.
The flip side: structural typing cannot distinguish two types that are structurally identical but semantically different, like UserId and ProductId both being number. For most code this is fine; when it is not, the 'branded types' pattern (covered in an advanced lesson) adds nominal-style distinctions.
interface Point {
x: number;
y: number;
}
interface Vector {
x: number;
y: number;
}
const p: Point = { x: 1, y: 2 };
const v: Vector = p; // OK: identical structure, freely interchangeable
function length(vec: Vector): number {
return Math.sqrt(vec.x * vec.x + vec.y * vec.y);
}
console.log(length(p)); // A Point works fine where a Vector is expected.Structure is all that matters: Point and Vector are interchangeable because their members match.
Declaration Merging and When to Use Interfaces
Interfaces have one unique superpower: declaration merging. If you declare the same interface name twice, TypeScript merges the declarations into one combined shape. This sounds odd for application code, and indeed you will rarely do it deliberately there, but it is the mechanism that lets you augment types from libraries, for example adding a custom property to Express's Request interface.
Merging is also a footgun: an accidental duplicate interface name quietly merges rather than erroring, which can hide mistakes. Type aliases, by contrast, refuse to be declared twice. This is one reason some teams default to type aliases for everything except public APIs.
Practical guidance: interfaces and type aliases overlap heavily for object shapes, and the next lesson covers aliases in depth. Prefer interfaces when you want extension, class implementation, or library augmentation. What matters most is picking one convention with your team and staying consistent.
interface Settings {
theme: string;
}
// Elsewhere (even another file): the SAME interface name merges.
interface Settings {
fontSize: number;
}
// The merged Settings now requires BOTH members:
const settings: Settings = { theme: "dark", fontSize: 14 };
console.log(settings.theme, settings.fontSize);Two declarations of Settings merge into one - useful for augmenting library types.
Try it yourself
This lesson's example is preloaded in the playground — break it, fix it, extend it.
Open in playgroundCheck your understanding
Question 1 · Multiple choice
An object literal is created without mentioning any interface, but its properties exactly match interface User. Can it be passed to a function expecting User?
Question 2 · Find the error
This code fails to compile. What is the problem?
interface Product {
readonly sku: string;
price: number;
}
const item: Product = { sku: "AB-1", price: 20 };
item.price = 25;
item.sku = "AB-2";
console.log(item);Question 3 · True or false
Declaring the same interface name twice in a project is a compile error, just as it is with type aliases.
Question 4 · Predict the output
What does this program print?
interface Animal {
name: string;
speak(): string;
}
interface Dog extends Animal {
breed: string;
}
const rex: Dog = {
name: "Rex",
breed: "Beagle",
speak() {
return this.name + " says woof";
},
};
function hear(a: Animal): void {
console.log(a.speak());
}
hear(rex);Question 5 · Multiple choice