# Type System Fundamentals

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

Canonical: https://www.internhack.in/learn/interview/typescript-interview/ts-type-system-basics
Last updated: 2026-11-28

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:

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

### 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

- Use `interface` for public object contracts that others may extend: library APIs, React component props in a design system, class contracts, global augmentation.
- Use `type` for unions, tuples, function types, utility-type compositions and anything computed with `keyof`, mapped or conditional types.
- Pick one convention for plain objects and enforce it with the `@typescript-eslint/consistent-type-definitions` rule. Consistency matters more than the choice itself.

### Common mistakes

- Believing interfaces are "faster at runtime". Both are erased completely; there is zero runtime difference.
- Trying to write `interface Status = 'paid' | 'failed'`. Unions need a type alias.
- Hover text: an interface shows its name in errors, while a complex type alias may be expanded inline, which can make error messages longer.

## 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

- Interfaces and type aliases have no runtime footprint; both are erased during compilation.
- Only interfaces support declaration merging, which powers global and library augmentation.
- Unions, tuples, mapped and conditional types require a type alias.
- \`extends\` reports property conflicts immediately; \`&\` turns them into never.
- A class can implement either, as long as the type resolves to an object shape.

## Common follow-up questions

- What is declaration merging and where have you used it?
- Can a class implement a type alias that is a union?
- Why might intersections be slower to type-check than interface inheritance?
- How would you add a custom property to Express's Request type?

## Interview tips

- Lead with the concrete differences (merging, unions, conflict detection), not "they are basically the same".
- Show the extends vs & conflict example; very few candidates know it and it signals real experience.
- End with the team convention you would pick and mention enforcing it with an ESLint rule.

## Concepts covered

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

Next: [Generics & Utility Types](https://www.internhack.in/learn/interview/typescript-interview/ts-generics-interview) · [All of TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)

## More TypeScript Interview questions

- [Type Guards & Narrowing](https://www.internhack.in/learn/interview/typescript-interview/ts-type-guards-narrowing)
- [Why TypeScript? JS vs TS](https://www.internhack.in/learn/interview/typescript-interview/ts-why-typescript)
- [Basic Types & Annotations](https://www.internhack.in/learn/interview/typescript-interview/ts-basic-types)
- [Arrays, Tuples & Enums](https://www.internhack.in/learn/interview/typescript-interview/ts-arrays-tuples-enums)
- [Functions & Overloads](https://www.internhack.in/learn/interview/typescript-interview/ts-functions-overloads)
- [Object Types & Optional Props](https://www.internhack.in/learn/interview/typescript-interview/ts-object-types)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)
