# Objects & Interfaces

> Two ways to define an object's shape, and the subtle difference between them.

Subject: TypeScript | Lesson 6 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/object-types-interfaces
Previous: [Arrays & Tuples](https://codewithanimation.in/typescript/arrays-and-tuples.md) | Next: [Enums vs Const Objects](https://codewithanimation.in/typescript/enums-vs-const-objects.md)

## Types vs Interfaces

When you need to define the shape of an object in TypeScript, you have two choices: a `type` alias or an `interface`.

For 95% of everyday code, they do exactly the same thing. You define the keys, the value types, and whether any keys are optional (using `?`).

## The Declaration Merging trick

The primary functional difference between the two is a feature called **Declaration Merging**.

If you declare an `interface` named `Window` and then declare *another* `interface` named `Window` lower down in the file, TypeScript will merge their properties together. If you try to do this with a `type` alias, TypeScript will throw a 'Duplicate identifier' error.

## Syntax comparison

Using an interface:

```typescript
interface User {
  id: number;
  name?: string; // Optional property
}
```

Using a type alias (note the equals sign):

```typescript
type User = {
  id: number;
  name?: string;
};
```

## Common mistake: Which one should I use?

Because they are so similar, teams often debate which one to use. If you use an `interface`, you can't use it to define unions (e.g., `type ID = string | number`).

Most modern style guides (like the TypeScript handbook itself) recommend starting with `interface` for objects, and using `type` aliases for everything else (unions, primitives, tuples).

## Tip: Extending third-party types

Declaration merging makes `interface` the perfect tool for augmenting third-party libraries. If you add a custom property to the global `window` object, you can just declare `interface Window { myCustomData: string }` and TypeScript will merge it with the browser's default Window object.

## Try it: Try it

Toggle between `type` and `interface` to see how TypeScript handles you trying to declare the exact same name twice.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/object-types-interfaces

## Check yourself

### Question 1

What happens if you declare two 'interface' blocks with the exact same name in the same scope?

- A. TypeScript throws a 'Duplicate identifier' error.
- B. TypeScript merges the properties of both interfaces into one.
- C. The second interface completely overrides the first one.

Correct answer: B

This feature is called Declaration Merging, and it's the main difference between an interface and a type alias.

- Why not A: That happens with 'type', not 'interface'.
- Why not C: TypeScript merges them; it does not overwrite.

## Remember this

- `interface` and `type` can both describe object shapes.
- `interface` supports Declaration Merging (re-declaring merges properties).
- `type` throws an error if you try to declare it twice.
- Use `type` when you need a union or intersection type.
