Simpler Expression of Complex Type Conditions #41123
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Search Terms
Multiple, type, condition, expression, no, extra, case, and, or
Suggestion
I often find myself repeating sub-branches of conditional types, which makes reasoning about control flow more difficult. I'd recommend the same logical operators to those of JS land, at the type-level.
Example
Let's say we have three boolean types––
ConditionA
,ConditionB
andConditionC
––and we want to gather the number of true conditions in a new typeResult
, which will evaluate to0 | 1 | 2 | 3
depending on the aforementioned conditions. The runtime equivalent can be expressed quite legibly:The type-level equivalent is not as obvious in its meaning, and we end up repeating the potential results.
While it nets more code, the following is––imo––easier to understand.
Ideally, the same rules from JS-land could apply to judging whether a type is a subtype of truthy, so we could do this:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: