Skip to content

sort-object-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 object types.

This rule standardizes the order of members of an object type in a TypeScript. The order in which the members are defined within an object type does not affect the type system or the behavior of the code.

Important

If you use the adjacent-overload-signatures rule from the @typescript-eslint/eslint-plugin plugin, it is highly recommended to disable it to avoid conflicts.

💡 Examples

ts
// ❌ Incorrect
type User = {
  isBlocked: boolean
  createdAt: Date
  role: 'admin' | 'lead' | 'user'
  username: string
  email: string
}

// ✅ Correct
type User = {
  createdAt: Date
  email: string
  isBlocked: boolean
  role: 'admin' | 'lead' | 'user'
  username: string
}
ts
// ❌ Incorrect
type User = {
  isBlocked: boolean
  createdAt: Date
  role: 'admin' | 'lead' | 'user'
  username: string
  email: string
}

// ✅ Correct
type User = {
  role: 'admin' | 'lead' | 'user'
  isBlocked: boolean
  username: string
  createdAt: Date
  email: string
}

🔧 Options

This rule accepts an options object with the following properties:

ts
type CustomGroup = string
type Group = 'multiline' | CustomGroup

interface Options {
  type?: 'alphabetical' | 'natural' | 'line-length'
  order?: 'asc' | 'desc'
  'ignore-case'?: boolean
  groups?: (Group | Group[])[]
  'custom-groups'?: { [key: Group]: string[] | string }
  'partition-by-new-line'?: 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.

groups

(default: [])

You can set up a list of type properties groups for sorting. Groups can be combined. There are predefined group: 'multiline'.

custom-groups

(default: {})

You can define your own groups for type properties attributes. The minimatch library is used for pattern matching.

Example:

{
  "custom-groups": {
    "callback": "on*"
  }
}

partition-by-new-line

(default: false)

When true, does not sort the object type's members if there is an empty string between them.

⚙️ Usage

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

export default [
  {
    plugins: {
      perfectionist,
    },
    rules: {
      'perfectionist/sort-object-types': [
        'error',
        {
          type: 'natural',
          order: 'asc',
          'always-on-top': ['id'],
        },
      ],
    },
  },
]

🚀 Version

This rule was introduced in v0.11.0.

📚 Resources

Released under the MIT License