Difficulty: Beginner
What is the difference between an interface and a type alias in TypeScript, and how do you decide which one to use?
An interface declares the shape of an object and can be extended and reopened (declaration merging). A type alias gives a name to any type at all: object shapes, but also unions, tuples, primitives, function signatures, mapped and conditional types. For plain object shapes they are almost interchangeable; the real differences are merging, what each can express, and how errors and performance behave with large hierarchies.
Both describe objects, and because TypeScript is structurally typed, an interface User and a type User with the same members are fully compatible. The gap is everything that is not an object shape:
| Capability | interface | type | |
|---|---|---|---|
| Object shapes, method signatures | Yes | Yes | |
| Unions like `'upi' \ | 'card'` | No | Yes |
| Tuples, primitives, function types | Awkward or no | Yes | |
| Mapped and conditional types | No | Yes | |
| Declaration merging (reopen later) | Yes | No, duplicate name is an error | |
| Extend another shape | extends | & intersection | |
Used with implements on a class | Yes | Yes, if it resolves to an object type |
If you write interface Window { razorpay: RazorpayCheckout } in a .d.ts file, TypeScript merges it into the built-in Window interface. This is how libraries let you add fields to Express.Request, how @types/node extends globals, and how plugin systems are typed. A type cannot do this. The flip side: merging can also happen by accident if two files in a global script declare the same interface name, which is one reason teams like type for internal app code.
interface Admin extends User checks compatibility at declaration time. If Admin redeclares id: string while User has id: number, you get an immediate error. With type Admin = User & { id: string } there is no error at the definition; id silently becomes number & string, which is never, and you only discover it when assigning a value. The TypeScript team also notes that interfaces with extends are cached by name and can be faster to check than deep chains of intersections in very large codebases.
interface for public object contracts that others may extend: library APIs, React component props in a design system, class contracts, global augmentation.type for unions, tuples, function types, utility-type compositions and anything computed with keyof, mapped or conditional types.@typescript-eslint/consistent-type-definitions rule. Consistency matters more than the choice itself.interface Status = 'paid' | 'failed'. Unions need a type alias.// Both describe the same object and are interchangeable here
interface UserI { id: number; name: string }
type UserT = { id: number; name: string };
const a: UserI = { id: 1, name: "Asha" };
const b: UserT = a; // OK: structural typing, names do not matter
// Only a type alias can express these
type PaymentMode = "upi" | "card" | "netbanking"; // union
type LatLng = [lat: number, lng: number]; // labelled tuple
type Handler = (e: MouseEvent) => void; // function type
type Nullable<T> = T | null; // generic alias
// Only an interface can be reopened (declaration merging)
interface UserI { email: string }
const c: UserI = { id: 2, name: "Ravi", email: "r@x.in" }; // email now required
For plain objects the two are compatible because TypeScript compares structure, not names. Unions, tuples and function types need a type alias, while reopening a declaration is interface only.
interface Base { id: number }
// Error right here: Interface 'BadAdmin' incorrectly extends 'Base'.
// Types of property 'id' are incompatible.
// interface BadAdmin extends Base { id: string }
type SneakyAdmin = Base & { id: string };
// No error yet... but id is now number & string = never
const admin: SneakyAdmin = {
id: 42, // Error: Type 'number' is not assignable to type 'never'
};
Interfaces validate the extension when you declare it. Intersections merge members without complaint, so a conflicting property quietly becomes never and the error appears far from its cause.
// global.d.ts
export {};
declare global {
interface Window {
Razorpay: new (options: { key: string; amount: number }) => {
open(): void;
};
}
}
// checkout.ts
const rzp = new window.Razorpay({ key: "rzp_test_123", amount: 49900 }); // paise
rzp.open();
Because Window is an interface, you can merge new members into it from your own declaration file. This is the main thing a type alias cannot do.
Interfaces, Type Aliases, Declaration Merging, Structural Typing, Intersection Types