Consider having two distinct types, named A
and B
. In TypeScript, the intersection type A & B
encompasses values that simultaneously possess characteristics of both types A
and B
.
The unique empty object type {}
is compatible with any structure resembling an object that can be accessed without triggering runtime errors. This implies that all object types, including most primitive types like string
, align with {}
. Notably, exceptions constitute only null
and undefined
, thus rendering {}
as a representation of "anything except for null
or undefined
."
Hence, the theoretical type undefined & {}
represents values embodying both undefined
and "anything except for null
or undefined
." Regrettably, no such values exist, signifying that undefined & {}
equates to an empty intersection symbolizing the inconceivable never type having zero attributes.
In practical terms, some instances may not immediately condense vacant intersections into never
in TypeScript. Nevertheless, any idle intersection containing at least one primitive type, such as undefined
, will indeed undergo such reduction by virtue of the modifications introduced in microsoft/TypeScript#31838. Consequently, undefined & {}
effectively amounts to never
.
Note that the NonNullable<T>
utility type has undergone revision since TypeScript 4.8, now redefined as T & {}
. Given that {}
signifies "everything excluding null
or undefined
," intersecting T
with {}
essentially results in a refined version of T
devoid of null
and undefined
.
Consequently, the application of NonNullable<undefined>
or NonNullable<null>
would yield never
, as eliminating these elements from themselves leaves behind emptiness.