Interfaces in TypeScript
How to define object shapes with interfaces, extend them, and use them with classes.
What you'll learn
- How to define an interface for an object shape
- Optional and readonly properties in interfaces
- How to extend interfaces to build on existing shapes
- When to choose interfaces over type aliases
Concept
Defining Object Shapes
An interface describes the shape of an object — which properties it has and their types. You define one with the interface keyword: interface User { id: number; name: string; }. Any object assigned to a User-typed variable must have those properties with matching types. Interfaces are the most common way to type objects, API responses, and configuration objects.
Optional and Readonly Properties
Mark a property as optional with a question mark: email?: string — the property may be absent. Mark a property as readonly with the readonly keyword: readonly id: number — it can be read but not reassigned after creation. These modifiers let you express intent: an ID should never change, and an email might not be set yet.
Extending Interfaces
Interfaces can extend one or more other interfaces, inheriting all their properties. interface Admin extends User { permissions: string[]; } has all of User's properties plus permissions. This composition lets you build complex types from simple ones. Interfaces are also mergeable — declaring the same interface name twice combines their properties, which is useful for extending library types.
Example
// Basic interface — describes the shape of a user object.
interface User {
readonly id: number;
name: string;
email?: string; // optional
isActive: boolean;
}
// Extending an interface — Admin has all User properties plus more.
interface Admin extends User {
permissions: string[];
lastLogin?: Date;
}
const user: User = { id: 1, name: "Alice", isActive: true };
const admin: Admin = {
id: 2,
name: "Bob",
isActive: true,
permissions: ["read", "write"],
};
// readonly prevents reassignment.
// user.id = 99; // ❌ Error: cannot assign to 'id' because it is readonly.
console.log(user.name, admin.permissions);
Interfaces describe object shapes. Optional (?) properties may be missing, readonly properties cannot be changed. extends inherits all properties from a parent interface.
Try it
- JSON to TypeScript
Automatically generate interfaces from JSON API responses.
- JSON Validator
Validate JSON against expected shapes before typing them.
Common mistakes
The mistake
Adding excess properties that the interface does not define
The fix
TypeScript checks for excess properties on object literals. If you need extra fields, extend the interface or use a type assertion for known additional data.
The mistake
Forgetting that interfaces are structural, not nominal
The fix
TypeScript uses structural typing — any object with the right shape matches the interface, even if it was not explicitly declared to implement it.
Practice Interfaces in TypeScript
8 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.