TS Conditional Type

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2021 Jun 7 9:17
Editor
Edited
Edited
2025 Oct 16 12:53
Refs
Refs
A method to perform branching while inferring types
type Example1 = Dog extends Animal ? number : string;
 
 
type Entry = "In" | "Out"; type InOrOut<Type extends `fade${string}`> = Type extends `fade${infer Return}` ? Return : never; // type I = "In" type In = InOrOut<"fadeIn">; // type O = "Out" type Out = InOrOut<"fadeOut">;
 
 

TS Recursive Conditional Type

define type recursive conditional

TypeScript 4.5 performs some tail-recursion elimination on conditional types
it doesn't error even with deep recursion and optimizes by avoiding intermediate instantiations
type TrimLeft<T extends string> = T extends ` ${infer Rest}` ? TrimLeft<Rest> : T; // Test = "hello" | "world" type Test = TrimLeft<" hello" | " world">; // error: Type instantiation is excessively deep and possibly infinite. type Test = TrimLeft<" oops">;
 
 
 
 
 
 

Recommendations