Q29: What are Conditional Types and how does the ternary syntax (`T extends U ? X : Y`) work?
Conditional types select one of two possible types based on a type relationship check, using a syntax similar to conditional ternary expressions:
T extends U ? TrueType : FalseType
Conditional Type Resolution
type IsString<T> = T extends string ? true : false;
type Test1 = IsString<string>; // true
type Test2 = IsString<number>; // false💡 If T is assignable to string, evaluate to true; otherwise evaluate to false.
Key Takeaways
- Conditional types enable dynamic compile-time decision logic.