CodePark/ questions / Conditional types & infer
START PRACTICINGPRACTICE
Frontend question · TypeScript › Conditional types & infer

What is A?

type IsNever<T> = T extends never ? true : false;
type A = IsNever<never>;
never

The answer is never. T is a naked type parameter, which means it stands alone in T extends never. So the conditional type is distributive: it runs once for each member of the union and joins the results.

TypeScript treats
never as an empty union, a union with zero members. Running the check once per member means running it zero times. Neither branch is ever evaluated. A union of zero results is never.

true is the tempting wrong answer, because never really is assignable to never. But the check never runs. To get true, wrap both sides in a tuple to stop distribution: [T] extends [never] ? true : false.