Type System Fundamentals

Difficulty: Beginner

Question

What is the difference between an interface and a type alias in TypeScript, and how do you decide which one to use?

Answer

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.

What each one can express

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:

Capabilityinterfacetype
Object shapes, method signaturesYesYes
Unions like `'upi' \'card'`NoYes
Tuples, primitives, function typesAwkward or noYes
Mapped and conditional typesNoYes
Declaration merging (reopen later)YesNo, duplicate name is an error
Extend another shapeextends& intersection
Used with implements on a classYesYes, if it resolves to an object type

Declaration merging and why it matters

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.

extends vs intersection

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.

A practical rule of thumb

Common mistakes

Code examples

Same shape, different capabilities

// 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.

extends catches conflicts, & hides them

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.

Augmenting a global with an interface

// 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.

Key points

Common follow-up questions

Interview tips

Concepts covered

Interfaces, Type Aliases, Declaration Merging, Structural Typing, Intersection Types

More TypeScript Interview questions