question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Questionable formatting for chained conditional types in TypeScript

See original GitHub issue

Prettier 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
suchipicommented, Sep 20, 2018

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.

1reaction
dfreemancommented, Sep 18, 2018

@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 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found