Skip to main content

Intersection of primitives

Short answer

If you combine incompatible types, for example:

javascript
type Impossible = string & number;

The result is the type never.


Why this happens

The & (intersection) operator means "both at the same time". TypeScript tries to build a type that matches all the combined types at once.

  • string means "any string"
  • number means "any number"

But a value cannot be a string and a number at the same time, so the intersection is empty, and TypeScript infers the type never.


Example 1. A direct intersection of incompatible types

javascript
type A = string & number; // => never let x: A; // x = "text"; error // x = 123; error // x = null; error

The type A equals never, meaning the value is impossible. TypeScript will not let you assign anything.


Example 2. An intersection of partially compatible types

Sometimes part of the properties overlaps, and TypeScript keeps only the shared fields.

javascript
type A = { id: number; name: string }; type B = { id: number; age: number }; type C = A & B; /* C = { id: number; // shared field name: string; // from A age: number; // from B } */ const person: C = { id: 1, name: "Tim", age: 25 }; // OK

Everything is compatible here, TypeScript just merged the properties.


Example 3. An intersection with conflicting property types

javascript
type A = { id: number }; type B = { id: string }; type C = A & B; // id must be both number and string -> incompatible

Now C["id"] = number & string -> never.

Therefore the whole type is:

javascript
type C = { id: never };

It is impossible to create a valid value:

javascript
const obj: C = { id: 123 }; // error const obj2: C = { id: "123" }; // error

Example 4. An intersection of union types

javascript
type A = string | number; type B = number | boolean; type C = A & B; // => number

Here TypeScript computes the common part of the two sets:

  • A = { string, number }
  • B = { number, boolean }
  • Intersection = { number }

Logical comparison

Operation typeSymbolLogic
`` (union)OR
& (intersection)ANDTakes only what fits everything at once

Summary

ExpressionResultExplanation
string & numberneverincompatible types
{ id: number } & { id: string }{ id: never }conflict on a field
{ name: string } & { age: number }{ name: string; age: number }compatible
`(stringnumber) & (numberboolean)`

In short:

If you combine types that cannot exist at the same time, TypeScript infers never - a type with no possible values.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.