Closed Howard-Lam-UnitedVanning closed 2 days ago
switch conditional type
A new way to define a generic type based on what another generic type extends by switch rather than chaining ternary operator
Current implementation
enum ModelType { DateTime, Date, Period, } type DateType< T extends (ModelType | undefined) = undefined> = T extends ModelType.Period ? DatePeriod : T extends ModelType.Date ? DateOnly : DateTime;
Potential new implementation
type DateType<T> = T switch { ModelType.Period => DatePeriod, ModelType.Date => DateOnly, _ => DateTime, }
One should also be able to do this with a list of strings
Duplicate of #51375.
Closing duplicate
π Search Terms
switch conditional type
β Viability Checklist
β Suggestion
A new way to define a generic type based on what another generic type extends by switch rather than chaining ternary operator
π Motivating Example
Current implementation
Potential new implementation
One should also be able to do this with a list of strings
π» Use Cases