Perfectionist

sort-object-types

Enforce sorted object types.

This rule standardizes the order of members within an object type in TypeScript. By ensuring that the members are sorted, it enhances readability without affecting the type system or code behavior.

This practice promotes a clear and consistent structure, making it easier for developers to understand and maintain object types.

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.

Try it out

Options

This rule accepts an options object with the following properties:

type

default: 'alphabetical'

Specifies the sorting method.

  • 'alphabetical' — Sort items alphabetically (e.g., “a” < “b” < “c”).
  • 'natural' — Sort items in a natural order (e.g., “item2” < “item10”).
  • 'line-length' — Sort items by the length of the code line (shorter lines first).

order

default: 'asc'

Determines whether the sorted items should be in ascending or descending order.

  • 'asc' — Sort items in ascending order (A to Z, 1 to 9).
  • 'desc' — Sort items in descending order (Z to A, 9 to 1).

ignoreCase

default: true

Controls whether sorting should be case-sensitive or not.

  • true — Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same).
  • false — Consider case when sorting (e.g., “A” comes before “a”).

partitionByNewLine

default: false

When true, the rule will not sort the members of an interface if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order.

type User = {
  // Group 1
  firstName: string;
  lastName: string;

  // Group 2
  age: number;
  birthDate: Date;

  // Group 3
  address: {
    street: string;
    city: string;
  };
  phone?: string;
};

In this example, the partitionByNewLine option will cause the rule to treat each group of members (separated by empty lines) independently, preserving their order within each group.

groupKind

default: 'mixed'

Allows you to group type object keys by their kind, determining whether required values should come before or after optional values.

  • mixed — Do not group object keys by their kind; required values are sorted together optional values.
  • required-first — Group all required values before optional.
  • optional-first — Group all optional values before required.

groups

default: []

Allows you to specify a list of type properties groups for sorting. Groups help organize properties into categories, making your type definitions more readable and maintainable. Multiple groups can be combined to achieve the desired sorting order.

There are predefined group: 'multiline'.

Predefined Group:

  • 'multiline' — Properties with multiline definitions, such as methods or complex type declarations.
  • 'unknown' — Properties that don’t fit into any other group.

customGroups

default: {}

You can define your own groups for type object members using custom glob patterns for matching.

Example:

 {
   groups: [
     'multiline',
     'unknown',
+    'callback',
   ],
+  customGroups: {   
+    callback: 'on*'
+  }                 
 }

Usage

Version

This rule was introduced in v0.11.0.

Resources

Table of Contents