Perfectionist

sort-interfaces

Enforce sorted TypeScript interface properties.

Sorting interface properties in TypeScript provides a clear and predictable structure to the codebase. This rule helps developers locate various properties defined within an interface more easily, promoting consistency and enabling efficient maintenance and collaboration.

Additionally, it ensures that property comments are sorted along with the properties themselves, preserving documentation clarity.

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.

It’s safe. If you document interface properties line by line, property comments will also be sorted.

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”).

ignorePattern

default: []

Allows you to specify names or patterns for interfaces that should be ignored by this rule. This can be useful if you have specific interfaces that you do not want to sort.

You can specify their names or a glob pattern to ignore, for example: 'Component*' to ignore all interfaces whose names begin with the word “Component”.

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.

interface User {
  // Group 1
  firstName: string;
  lastName: string;

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

  // Group 3
  address: 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'

Specifies how optional and required members should be ordered in TypeScript interfaces.

  • 'optional-first' — Put all optional members before required members.
  • 'required-first' — Put all required members before optional members.
  • 'mixed' — Do not enforce any specific order based on optionality.

groups

default: []

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

There are predefined group: 'multiline'.

Predefined Groups:

  • 'multiline' — Members with multiline definitions, such as methods or properties with complex types.
  • 'unknown' — Interface members that don’t fit into any other group.

customGroups

default: {}

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

Example:

 {
   groups: [
+    'top',
     'multiline',
     'unknown',
     ['shorthand', 'astro-shorthand'],
   ],
+  customGroups: {   
+    top: 'id'
+  }                 
 }

Usage

Version

This rule was introduced in v0.1.0.

Resources

Table of Contents