# Intersection Types

> Combining multiple types into a single, merged super-type.

Subject: TypeScript | Lesson 11 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/intersection-types
Previous: [Union Types](https://codewithanimation.in/typescript/union-types.md) | Next: [The typeof Operator](https://codewithanimation.in/typescript/typeof-operator.md)

## The AND Operator for Types

While a Union (`|`) means 'one or the other', an Intersection (`&`) means 'both at the same time'.

When you intersect two object types, the resulting type is a single object containing all the properties from both original types.

## Mixing and extending

Intersection types are incredibly useful for composition. Instead of writing massive, monolithic types, you can write small, focused types and combine them as needed.

For example, in React, you might combine native button props with your own custom props: `type ButtonProps = React.ComponentProps<"button"> & { variant: 'primary' | 'secondary' };`

## Creating an Intersection

You define an intersection using the ampersand (`&`).

```typescript
type Name = { name: string };
type Age = { age: number };

type Person = Name & Age;

// Person is equivalent to: { name: string; age: number }
const p: Person = { name: "Alice", age: 30 };
```

## Common mistake: The accidental 'never' trap

What happens if you intersect two types that have the *same* property key, but completely different primitive types?

```typescript
type A = { id: string };
type B = { id: number };

type C = A & B;
```

Because a value cannot be both a `string` AND a `number` simultaneously, TypeScript silently resolves the `id` property to `never`. When you try to use it, you'll get bizarre errors saying `Type 'X' is not assignable to type 'never'`.

## Tip: Interfaces vs Intersections

When composing objects, it is often safer and slightly more performant for the compiler to use `interface A extends B` rather than `type A = B & C`. Interfaces will actively throw an error if properties conflict, rather than silently creating a `never` type.

## Try it: Try it

Watch two separate types merge into a single, comprehensive super-type using the `&` operator.

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

## Check yourself

### Question 1

What happens if you intersect two object types that share a property key, but have incompatible primitive types for that property?

- A. TypeScript throws a compile error immediately.
- B. The conflicting property is silently typed as 'never'.
- C. The second type overwrites the first type.

Correct answer: B

TypeScript tries to intersect the two conflicting primitive types (e.g. string & number). Since nothing can be both, it resolves to 'never'.

- Why not A: Interfaces do this, but intersections (&) do not.
- Why not C: That is how Object.assign works in JavaScript, not how types work in TypeScript.

## Remember this

- An Intersection Type (`A & B`) creates a type with all the properties of both `A` and `B`.
- They are excellent for composing small types into larger ones.
- Conflicting primitives in an intersection result in a `never` type.
- Prefer `interface extends` for object composition when possible.
