Sub topic 8
Conflicting Members and Intersections That Become never
This chapter assumes you know how to write object types and type aliases, and that you have seen the basic A & B syntax for intersections. It covers what happens when the types you combine disagree. Sometimes a single property turns into never. Sometimes the whole intersection turns into never. Sometimes nothing breaks at all. Knowing which case you are in saves a lot of debugging time.
Why an intersection can be empty
An intersection A & B describes values that satisfy both A and B. When no value can satisfy both, the type is empty. TypeScript represents the empty type as never, the type with no values.
The simplest case is two different primitive types:
type Impossible = string & number;
declare const value: Impossible;
const n: never = value;The assignment to never compiles because Impossible is never. No value is both a string and a number, so TypeScript reduces the intersection immediately instead of keeping the name string & number.
Literal types behave the same way. Each literal type contains exactly one value, so two different literals cannot overlap:
type Same = "a" & string; // "a"
type Clash = "a" & "b"; // never
const ok: Same = "a";
declare const clash: Clash;
const alsoNever: never = clash;"a" & string gives "a" because every value of "a" is already a string. When one type is a subtype of the other, the intersection is the narrower type. When the two types share no values, the result is never.
Conflicting properties in object types
Object types make the situation less obvious, because an object type can still exist when one of its properties cannot.
type A = { id: string };
type B = { id: number };
type C = A & B;
declare const c: C;
const id = c.id; // type: neverTypeScript intersects the type of each shared property. Here id becomes string & number, which is never. The object type C is not reduced to never as a whole, so C is still a valid type name and you can declare variables of it. The practical result is that you cannot build a value of that type:
// Error: Type 'string' is not assignable to type 'never'.
const attempt: C = { id: "abc" };The error points at id, not at C. That can be confusing when C comes from a generic helper and you never wrote it out yourself.
Conflicting discriminants make the whole type never
There is one exception. If the conflicting property is a discriminant (a property whose type is a literal type, or a union of literal types, used as a tag), TypeScript reduces the entire object intersection to never:
type Ok = { status: "ok"; data: string };
type Failed = { status: "error"; message: string };
type Both = Ok & Failed;
declare const both: Both;
const n: never = both;The reduction is deliberate. Discriminated unions rely on the tag property to tell members apart, so an object with two different tags cannot exist. Reducing the whole type to never lets filtering and narrowing work correctly.
Note the difference between the two cases:
{ id: string } & { id: number }keeps its shape, andidisnever.{ status: "ok" } & { status: "error" }isneverentirely.
The rule is based on the property types. A property typed as a plain string is not a discriminant, so a clash there only poisons that property.
A summary of outcomes
| Intersection | Result | Column |
|---|---|---|
| `string & number` | `never` | |
| `"a" & "b"` | `never` | |
| `"a" & string` | `"a"` | |
| `{ kind: "a" } & { kind: "b" }` | `never` | |
| `{ id: string } & { id: number }` | object where `id` is `never` | |
| `{ id: string \ | number } & { id: string }` | object where `id` is `string` |
| `{ a?: string } & { a: string }` | object where `a` is a required `string` |
The sixth and seventh rows show the useful side of the rule. A property in an intersection has the intersection of its property types, so overlapping types narrow the property, and a required property wins over an optional one.
Not every clash is a clash
Some combinations look like conflicts and are not.
Methods become overloads
If two object types declare the same method with different signatures, the property type is an intersection of function types. An intersection of functions is not empty. It behaves like an overloaded function:
type A = { get(): string };
type B = { get(): number };
declare const ab: A & B;
const s = ab.get(); // string, the first matching signatureThe call matches the first signature, so you get string. Overload order follows the order of the intersection, so B & A would give number. A later chapter covers this in more detail.
Primitives with object types
A primitive intersected with an object type is not reduced. This is the basis of the "branded type" pattern:
type UserId = string & { readonly __brand: "UserId" };
function asUserId(raw: string): UserId {
return raw as UserId;
}
function loadUser(id: UserId): string {
return `user:${id}`;
}
loadUser(asUserId("u-1"));
// loadUser("u-1"); // Error: string is not assignable to UserIdA plain string is not assignable to UserId, but a UserId still works anywhere a string is expected. The brand property exists only in the type system.
Optional properties
When both sides declare an optional property with unrelated types, the property type becomes never, but the property stays optional. In practice the only possible value is undefined, or leaving the property out. Nothing reduces to never at the object level, and no error is reported at the declaration.
Why a silent never is dangerous
A value of type never is assignable to every type. That is correct for unreachable code, but it hides mistakes when a conflicting property produces it:
type Base = { id: string; name: string };
type Override = { id: number };
type Wrong = Base & Override;
declare const w: Wrong;
const label: string = w.id; // compiles, because never is assignable to string
const total: number = w.id; // also compilesBoth assignments compile because w.id is never. The type of Wrong is useless, and the compiler is not complaining about reading it. The error only shows up when you try to construct a Wrong, which may be in a different file from where the mistake was made.
Replacing a property instead of intersecting it
If you want to override a property, intersecting is the wrong tool. Remove the old property first, then add the new one:
type Base = { id: string; name: string };
type Override = { id: number };
type Right = Omit<Base, keyof Override> & Override;
const r: Right = { id: 42, name: "Ada" };Omit<Base, keyof Override> is { name: string }. Intersecting it with Override yields { name: string } & { id: number }, which has no conflicts.
Detecting never
When you write generic helpers that combine types, you can add a check that fails loudly. The check below wraps T in a one-element tuple. Without the wrapping, a bare never used with a conditional type produces never instead of true. The distribution chapter explains why.
type IsNever<T> = [T] extends [never] ? true : false;
const clash: IsNever<string & number> = true;
const tagClash: IsNever<{ kind: "a" } & { kind: "b" }> = true;
const fine: IsNever<{ a: string } & { b: number }> = false;Each assignment compiles only if the helper returns the type on the left. If someone changes a type so that a previously valid combination collapses, the matching line stops compiling.
Note that IsNever<{ id: string } & { id: number }> is false, because only the property is never. To catch property-level conflicts, check the property directly: IsNever<C["id"]>.
never inside unions and during narrowing
never interacts with unions in a way that mirrors how it interacts with intersections:
T | neverisT. A union ignoresnevermembers.T & neverisnever. An intersection withneveris alwaysnever.
This is why filtering types works. A conditional type can map unwanted members to never, and they vanish from the resulting union.
Narrowing uses the same rule. When you narrow a union by removing members until none remain, the variable becomes never in that branch. The exhaustiveness technique from the earlier chapter relies on this. A never that appears where you did not expect it is a signal that an earlier check or type has already eliminated every possibility.
Practical rules
- Use
&to add members. UseOmitplus&, or a mapped type, to replace members. - Treat conflicting literal tags as a sign you wanted a union (
|), not an intersection. - Expect overlapping property types to narrow, not clash.
string | numberintersected withstringisstring. - Use branding (primitive & object) for nominal-style types, and do not expect it to reduce.
- Add
IsNevertests for generic utilities that combine caller-supplied types.
Exercise
Predict the type of each of these without running the compiler, and say whether the whole type is never or only a property: (1) { kind: "x"; n: number } & { kind: "y" }, (2) { n: number } & { n: string }, (3) { n: number | string } & { n: string }.
Show answer
(1) The whole type is never, because kind is a literal tag and the two literals "x" and "y" do not overlap. (2) The type is an object where n is never, because number & string is never but n is not a literal tag. Only the property is affected, and you cannot construct a value of this type. (3) The type is an object where n is string, because (number | string) & string is string.
Exercise
A teammate wrote type Config = Defaults & UserConfig, where Defaults has port: number and UserConfig has port: string because it is read from an environment variable. Reading config.port compiles, but building a Config fails with "Type 'string' is not assignable to type 'never'". Explain the cause and give a fix that produces a port of type string.
Show answer
The intersection makes port the type number & string, which is never. Reading it compiles because never is assignable to everything, and writing fails because nothing can be assigned to never. Replace the property instead of intersecting it: type Config = Omit<Defaults, "port"> & UserConfig. This removes port from Defaults before adding the string version from UserConfig.