Skip to content

Bug: [sort-type-constituents] Auto-fix can change behavior #6339

Closed
@ersimont

Description

@ersimont

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.9.3&sourceType=module&code=C4TwDgpgBAQgTgewNYQHYB4AqA+KBeAWACgooAfKACkyggA9g0ATAZylQFcBbAIwjigB+KAHIAFgEsRUAFyjgY-hBEBKYqQotgcCagDmAbmJA&eslintrc=N4KABGBEBOCuA2BTAzpAXGYUACAXAngA4oDG0AlobgLQrzkB2uA9MgPbQ0HHUlsPJc5XLERNUGANqRE0aB0gBdMAF8QKoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

type Broken<T> =
  | (T extends number ? 'hi' : 'there')
  | string;

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/<rule-name>": ["error", ...<options>],
  },
};

tsconfig

{
  "compilerOptions": {}
}

Expected Result

When using "fix", I expect it to keep the same semantics of the code. This might include keeping the () around the T extends clause.

Also I expect the "fix" to be stable, so that it no longer reports an error.

Actual Result

It fixes to this, which changes the meaning of the code:

type Broken<T> =
  string | T extends number ? 'hi' : 'there';

Also, it still complains about the order. If you fix again it changes to

type Broken<T> =
  T | string extends number ? 'hi' : 'there';

Additional Info

I don't know/care what order it sorts to, as long as it doesn't break my code! 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions