Skip to content

sort-intersection-types

💼 This rule is enabled in the following configs: recommended-alphabetical, recommended-line-length, recommended-natural.

🔧 This rule is automatically fixable by the --fix CLI option.

📖 Rule Details

Enforce sorted intersection types in TypeScript.

Adhering to the sort-intersection-types rule enables developers to ensure that intersection types are consistently sorted, resulting in cleaner and more maintainable code. This rule promotes a standardized ordering of intersection types, making it easier for developers to navigate and understand the structure of type intersections within the codebase.

Important

If you use the sort-type-constituents rule from the @typescript-eslint/eslint-plugin plugin, it is highly recommended to disable it to avoid conflicts.

💡 Examples

ts
// ❌ Incorrect
type NetworkState =
  & Failed
  & LoadedFromCache
  & Success
  & DataLoading

// ✅ Correct
type NetworkState =
  & DataLoading
  & Failed
  & LoadedFromCache
  & Success
ts
// ❌ Incorrect
type NetworkState =
  & Failed
  & LoadedFromCache
  & Success
  & DataLoading

// ✅ Correct
type NetworkState =
  & LoadedFromCache
  & DataLoading
  & Success
  & Failed

🔧 Options

This rule accepts an options object with the following properties:

ts
interface Options {
  type?: 'alphabetical' | 'natural' | 'line-length'
  order?: 'asc' | 'desc'
  'ignore-case'?: boolean
}

type

(default: 'alphabetical')

  • alphabetical - sort alphabetically.
  • natural - sort in natural order.
  • line-length - sort by code line length.

order

(default: 'asc')

  • asc - enforce properties to be in ascending order.
  • desc - enforce properties to be in descending order.

ignore-case

(default: false)

Only affects alphabetical and natural sorting. When true the rule ignores the case-sensitivity of the order.

⚙️ Usage

json
// .eslintrc
{
  "plugins": ["perfectionist"],
  "rules": {
    "perfectionist/sort-intersection-types": [
      "error",
      {
        "type": "natural",
        "order": "asc"
      }
    ]
  }
}
js
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'

export default [
  {
    plugins: {
      perfectionist,
    },
    rules: {
      'perfectionist/sort-intersection-types': [
        'error',
        {
          type: 'natural',
          order: 'asc',
        },
      ],
    },
  },
]

🚀 Version

This rule was introduced in v2.9.0.

📚 Resources

Released under the MIT License