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.

Ugly formatting of nested conditional types in TypeScript

See original GitHub issue

Prettier 2.0.2 Playground link

--parser typescript
--print-width 100
--trailing-comma all

Input:

type TypeName<T> =
  T extends string ? "string" :
  T extends number ? "number" :
  T extends boolean ? "boolean" :
  T extends undefined ? "undefined" :
  T extends Function ? "function" :
  T extends Array<any> ? "array" :
  T extends null ? "null" :
  T extends symbol ? "symbol" :
  "object";

Output:

type TypeName<T> = T extends string
  ? "string"
  : T extends number
  ? "number"
  : T extends boolean
  ? "boolean"
  : T extends undefined
  ? "undefined"
  : T extends Function
  ? "function"
  : T extends Array<any>
  ? "array"
  : T extends null
  ? "null"
  : T extends symbol
  ? "symbol"
  : "object";

Expected behavior:

Keep the original formatting which is much nicer.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:50
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
alexander-akaitcommented, Apr 2, 2020

I think we should improve it

0reactions
MichaelDeBoeycommented, Apr 17, 2021

Prettier 2.2.1 Playground link

Input:

const isNodeOrTokenOfTypeWithConditions = <
  NodeOrToken extends TSESTree.Node | TSESTree.Token,
  NodeOrTokenType extends (NodeOrToken extends TSESTree.Node ? AST_NODE_TYPES : AST_TOKEN_TYPES),
  Conditions extends Partial<NodeOrToken & { type: NodeOrTokenType }>
>(
  nodeOrTokenType: NodeOrTokenType,
  conditions: Conditions
) => (nodeOrToken: NodeOrToken) => true;

Output:

const isNodeOrTokenOfTypeWithConditions = <
  NodeOrToken extends TSESTree.Node | TSESTree.Token,
  NodeOrTokenType extends NodeOrToken extends TSESTree.Node
    ? AST_NODE_TYPES
    : AST_TOKEN_TYPES,
  Conditions extends Partial<NodeOrToken & { type: NodeOrTokenType }>
>(
  nodeOrTokenType: NodeOrTokenType,
  conditions: Conditions
) => (nodeOrToken: NodeOrToken) => true;

Expected behavior: At least keep the parentheses around NodeOrToken extends TSESTree.Node ? AST_NODE_TYPES : AST_TOKEN_TYPES

Read more comments on GitHub >

github_iconTop Results From Across the Web

Evaluation of Nested Conditional Type - Stack Overflow
The conditional type you've got here, type OrganizationPayload<TR extends string | null = string | null> = { id: number districtAdmin?:
Read more >
Google TypeScript Style Guide
TypeScript expresses information in types, so names should not be decorated with information that is included in the type. (See also Testing Blog...
Read more >
Advanced TypeScript: The ultimate Tailwind typings
This post is about implementing utilities for the ultimate Tailwind-React ergonomics and typings for it. In the process, we'll learn about ...
Read more >
Are chain ternary operators an antipattern? : r/reactjs - Reddit
In JavaScript the ternary operators are parsed right-to-left, ... Also, nested ternaries if formatted flat and well considered are totally ...
Read more >
Pandoc User's Guide
Pandoc is a Haskell library for converting from one markup format to another, ... Thus, to convert hello.txt from Markdown to LaTeX, you...
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