Questionable formatting for chained conditional types in TypeScript
See original GitHub issuePrettier 1.14.2 Playground link
--parser typescript
Input:
let x = one instanceof two ? three : four instanceof five ? six : seven instanceof eight ? nine : ten;
type x = one extends two ? three : four extends five ? six : seven extends eight ? nine : ten;
Output:
let x =
one instanceof two
? three
: four instanceof five
? six
: seven instanceof eight
? nine
: ten;
type x = one extends two
? three
: four extends five ? six : seven extends eight ? nine : ten;
Expected behavior:
I know there’s some open discussion in https://github.com/prettier/prettier/issues/737 around exactly how chained ternary expressions should be formatted, but no matter the outcome there, I think I would expect chained conditional types to be formatted the same way.
It seems as though the formatting for chained conditional types prefers to keep as much content on the final line as possible, which ends up giving more visual weight to some branches than others and leading to some weird readability issues. As far as I can tell, chained ternaries don’t exhibit this behavior—as soon as the chain is forced to break across lines, the whole thing cascades.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Documentation - Conditional Types - TypeScript
Create types which act like if statements in the type system.
Read more >TypeScript and JavaScript conditional chaining
The phrase "conditional chaining" is a misnomer used to describe two features - optional chaining and non-null assertion chaining. Both don't ...
Read more >TypeError: cannot use 'in' operator to search for 'x' in 'y'
The in operator can only be used to check if a property is in an object. You can't search in strings, or in...
Read more >Typescript :: Conditional chaining function - Stack Overflow
I want to archive type check of the function in the chain. In the above example, I want the functions specialPlus and specialMinus...
Read more >Chapter 4. Types, Values, and Variables - Oracle Help Center
The conditional operator ? ... Floating-Point Types, Formats, and Values ... The conditional-and and conditional-or operators && (§15.23) and || (§15.24).
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry for the late response, I’ve been pretty busy. My intention with that PR was to treat conditional types the same way, so I would consider it in-scope.
@suchipi Just a friendly ping—totally understand if this is out of scope for #5039, but want to make sure this doesn’t get lost if that’s the case 🙂