useSortedInterfaceMembers (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.4.0 - Diagnostic Category:
assist/source/useSortedInterfaceMembers - Sources:
- Inspired from
perfectionist/sort-interfaces - Inspired from
typescript-sort-keys/interface
- Inspired from
How to enable in your editor
Section titled “How to enable in your editor”{ "editor.codeActionsOnSave": { "source.action.useSortedInterfaceMembers.biome": "explicit", "source.fixAll.biome": "explicit" }}{ "code_actions_on_format": { "source.action.useSortedInterfaceMembers.biome": true, "source.fixAll.biome": true }}source.action.useSortedInterfaceMembers.biome How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedInterfaceMembers": "on" } } }}Description
Section titled “Description”Sort named members of a TypeScript interface.
The action uses natural order, which compares numbers by value so that item9 comes before
item10. It can sort members with fixed names:
- properties:
property: type; - methods:
method(): type; - getters:
get property(): type; - setters:
set property(value: type): void.
Members without a fixed name cannot be sorted by name:
- call signatures such as
(): typemake the interface callable; - construct signatures such as
new (): typemake it constructible; - index signatures such as
[key: string]: typedescribe dynamically named properties; - computed names such as
[Symbol.iterator]()are calculated from an expression.
The action places sorted, named members first. It keeps members without fixed names in their original relative order after them.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”interface MixedMembers { z: string; a: number; (): void; // Call signature y: boolean; new (): MixedMembers; // Construct signature b: string; [key: string]: any; // Index signature}code-block.ts:1:1 assist/source/useSortedInterfaceMembers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The interface members are not sorted by key.
> 1 │ interface MixedMembers {
│ ^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ z: string;
> 3 │ a: number;
> 4 │ (): void; // Call signature
> 5 │ y: boolean;
> 6 │ new (): MixedMembers; // Construct signature
> 7 │ b: string;
> 8 │ [key: string]: any; // Index signature
> 9 │ }
│ ^
10 │
ℹ Safe fix: Sort the interface members by key.
1 1 │ interface MixedMembers {
2 │ - ··z:·string;
3 │ - ··a:·number;
4 │ - ··():·void;··//·Call·signature
5 │ - ··y:·boolean;
6 │ - ··new·():·MixedMembers;··//·Construct·signature
7 │ - ··b:·string;
2 │ + ··a:·number;
3 │ + ··b:·string;
4 │ + ··y:·boolean;
5 │ + ··z:·string;
6 │ + ··():·void;··//·Call·signature
7 │ + ··new·():·MixedMembers;··//·Construct·signature
8 8 │ [key: string]: any; // Index signature
9 9 │ }
interface MixedMembers { a: number; b: string; y: boolean; z: string; (): void; // Non-sortable members remain in original order new (): MixedMembers; [key: string]: any;}Options
Section titled “Options”partitionByNewLine
Section titled “partitionByNewLine”When enabled, members separated by a blank line are kept in their own section and sorted only within that section. This preserves logical groupings that the author intentionally introduced with empty lines.
Default:
false
{ "assist": { "actions": { "source": { "useSortedInterfaceMembers": { "level": "on", "options": { "partitionByNewLine": true } } } } }}With the option enabled, the following interface is considered sorted because each section is sorted on its own:
interface User { id: string;
createdAt: Date; updatedAt: Date;
email: string; name: string; passwordHash: string;}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.