What Interfaces Are
You'll learn: What interfaces do in TypeScript and how they define the expected structure of objects.
The Core Idea
An interface is TypeScript's way of giving a name to an object's shape. It declares what properties and methods an object should have, along with their types, without providing any implementation. Think of an interface like a blueprint or contract—it specifies what must exist, but not how it works.
You've already been working with object type literals throughout the course. An interface simply lets you name that object structure so you can reuse it and refer to it clearly.
Defining an Interface
interface User {
name: string;
age: number;
}
This interface says: "Any object that claims to be a User must have a name (string) and an age (number)."
Using an Interface
interface User {
name: string;
age: number;
}
const alice: User = {
name: "Alice",
age: 30
};
function greet(user: User) {
return `Hello, ${user.name}`;
}
greet(alice); // Works perfectly
The interface acts as a type annotation. TypeScript checks that alice matches the User shape and that greet receives a compatible object.
Why Interfaces Matter
Instead of repeating the same object type literal everywhere, you define it once with a clear name. This makes your code easier to read, maintain, and refactor. Interfaces also support all the features you've learned: optional properties, readonly modifiers, function properties, and index signatures.
Key Takeaway: Interfaces define named contracts for object shapes, enabling reusable, self-documenting type-checking across your codebase.