Skip to content

Conditional types in generic constraints look confusing #10732

Description

@thorn0

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

Originally posted by @MichaelDeBoey in #7940 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty:easyIssues that should be easy to fix. Resolve me on the train to work!good first issueGood fist issue!help wantedWe're a small group who can't get to every issue promptly. We’d appreciate help fixing this issue!lang:flowIssues affecting Flow-specific constructs (not general JS issues)lang:typescriptIssues affecting TypeScript-specific constructs (not general JS issues)locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions