Why can't void and {} be inferred as never type in TypeScript?
P粉548512637
2023-09-04 16:03:52
<p>I get different results when I use TypeScript's void type with other types using cross types. </p>
<pre class="brush:php;toolbar:false;">type A = void & {} // A is void & {}
type B = void & '1' // B is never
type C = void & 1 // C is never
type D = void & string // D is never
type E = void & String // E is void & String</pre>
<pre class="brush:php;toolbar:false;">type A = void & {}
type E = void & String</pre>
<p>They should also be of type never, right? </p>
{}
andString
are both object types, whilestring
and'1'
are both primitive types. You can intersectvoid
with object types because object types intersect by adding properties:In contrast, primitive types intersect by reducing the set of possible values:
And by intersecting a primitive type with an object type, you can add new properties to the primitive type:
But a primitive type can never become another primitive type. Therefore, intersecting two different primitive types will result in
never
Finally,
void
is a primitive type.So, this means
void & { foo: number }
means that the primitive typevoid
will also have the attributefoo
.However,
void & string
will producenever
since they are two different primitive types.However,
void & String
are properties ofvoid
plusString
, becauseString
is an object type (vianew String()
Create).However, all this means nothing. You cannot assign
void
to anything other thanundefined
, andundefined
cannot have properties. So I thinkvoid & Type
has no reason to exist in your codebase. If you think you need it, I would ask you why you need it and try to refactor the code so that it doesn't need it.