Interface vs Object Type Literal
What you'll learn: How interfaces differ from object type literals and when to use each.
The Core Difference
At their heart, interfaces are named types — they give a name to a shape, which you can then reference throughout your code. Object type literals, on the other hand, are anonymous inline shapes — they describe an object's structure directly where they're used, without giving that structure a name.
Think of it like the difference between defining a blueprint with a label ("this is a House blueprint") versus describing what you need on the spot ("I need something with a door and two windows").
Named vs Anonymous
// Interface: Named type
interface User {
name: string;
age: number;
}
// Object Type Literal: Anonymous inline shape
let admin: { name: string; age: number };
Both describe the exact same shape — an object with name and age properties. The difference is purely in how you declare them. The interface has a name (User) that you can reuse. The object type literal is written directly in place.
When to Use Each
Use interfaces when:
- You'll reference the same shape multiple times
- You want clear, self-documenting type names
- You're describing public APIs or contracts
Use object type literals when:
- You need a one-off type for a single use
- The shape is simple and self-explanatory in context
Both work identically for basic object typing — this is purely a choice about naming and reusability.
Key Takeaway: Interfaces give object shapes a reusable name; object type literals describe shapes anonymously inline. They're interchangeable for simple cases, with interfaces offering better reusability.