I believe this is a consequence of the distributive property of conditional types in Typescript. Essentially, (S | T) extends A ? B : C
is equivalent to (S extends A ? B : C) | (T extends A ? B : C)
, i.e. the conditional type distributes over the union in the extends
clause. Since every type T
is equivalent to T | never
, it follows that
T extends A ? B : C
is equivalent to(T | never) extends A ? B : C
,- Which in turn is equivalent to
(T extends A ? B : C) | (never extends A ? B : C)
, - This is the same as the original type unioned with
never extends A ? B : C
.
So a conditional type of the form never extends A ? B : C
must evaluate to never
, otherwise the distributive property would be violated.
To make your type work as intended, you can use this trick:
type TypeCond<T, U> = [T] extends [never] ? {a: U} : {b: U};
This avoids the conditional type distributing over T
, since [T]
is not a “naked type parameter”.