The unexpected way in which conditional types constrain type variables in TypeScript
Briefly

This article explores the complexities of conditional types in TypeScript, illustrating how they can provide deeper insights and constraints on generics. It emphasizes the distributive property of conditional types and how this affects type comparisons, often yielding results that seem paradoxical, such as both true and false. The author presents examples and hypotheses to explain the nuanced behavior of these types, particularly regarding the appearance of 'never' in results, showcasing the sophisticated capabilities of TypeScript's type system beyond mere narrowing of types.
Often, the checks in a conditional type will provide us with some new information. Just like narrowing with type guards can give us a more specific type, the true branch of a conditional type will further constrain generics by the type we check against.
Conditional types are distributive over the left-hand side of extends. The results of comparisons often yield both true and false due to this distributive property.
A conditional type's ability to narrow types allows for operations that would otherwise be invalid on arbitrary types, such as retrieving length.
The complexity of constraints imposed by conditional types reveals that they're more intricate than just simple narrowing, leading to unexpected outcomes like 'never' appearing in tuples.
Read at 2ality
[
|
]