ESLint plugin for TypeScript that enforces consistent sorting of interface keys and string enum members
npx @tessl/cli install tessl/npm-eslint-plugin-typescript-sort-keys@3.3.0ESLint plugin for TypeScript that enforces consistent sorting of interface keys and string enum members. This plugin provides two main rules that automatically fix sorting issues and integrates seamlessly with existing ESLint configurations.
npm install --save-dev eslint-plugin-typescript-sort-keyseslint: ^7 || ^8@typescript-eslint/parser: >=6typescript: ^3 || ^4 || ^5>= 16Plugin Import (Node.js):
// ESM
import plugin from 'eslint-plugin-typescript-sort-keys';
// CommonJS
const plugin = require('eslint-plugin-typescript-sort-keys');ESLint Configuration:
// .eslintrc.js
module.exports = {
plugins: ['typescript-sort-keys'],
extends: ['plugin:typescript-sort-keys/recommended'],
rules: {
'typescript-sort-keys/interface': 'error',
'typescript-sort-keys/string-enum': 'error'
}
};// .eslintrc.json
{
"plugins": ["typescript-sort-keys"],
"extends": ["plugin:typescript-sort-keys/recommended"],
"rules": {
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error"
}
}// Before: Unsorted interface
interface User {
name: string;
age: number;
email: string;
id: number;
}
// After: Sorted interface (auto-fixed)
interface User {
age: number;
email: string;
id: number;
name: string;
}
// Before: Unsorted string enum
enum Status {
PENDING = 'pending',
ACTIVE = 'active',
CANCELLED = 'cancelled',
COMPLETED = 'completed'
}
// After: Sorted string enum (auto-fixed)
enum Status {
ACTIVE = 'active',
CANCELLED = 'cancelled',
COMPLETED = 'completed',
PENDING = 'pending'
}The plugin consists of:
Default export providing the plugin configuration with all rules and preset configurations.
const plugin: {
rules: {
interface: ESLintRule;
'string-enum': ESLintRule;
};
configs: {
recommended: RecommendedConfig;
};
};
export default plugin;Enforces consistent sorting of TypeScript interface properties and type literal properties. Supports both TSInterfaceDeclaration and TSTypeLiteral nodes.
type InterfaceRuleOptions = [
SortingOrder,
Partial<{
caseSensitive: boolean;
natural: boolean;
requiredFirst: boolean;
}>?
];
type SortingOrder = 'asc' | 'desc';Enforces consistent sorting of TypeScript string enum members. Only applies to enums where all members have string literal initializers.
type StringEnumRuleOptions = [
SortingOrder,
Partial<{
caseSensitive: boolean;
natural: boolean;
}>?
];Preset ESLint configuration that enables both rules with error level severity.
const recommendedConfig = {
plugins: ['typescript-sort-keys'],
rules: {
'typescript-sort-keys/interface': 'error',
'typescript-sort-keys/string-enum': 'error'
}
};import { JSONSchema4 } from 'json-schema';
import {
RuleContext,
RuleListener,
RuleModule
} from '@typescript-eslint/experimental-utils/dist/ts-eslint';
enum SortingOrder {
Ascending = 'asc',
Descending = 'desc'
}
type SortingOrderOption = SortingOrder;
interface SortingParamsOptions {
readonly caseSensitive: { readonly caseSensitive: boolean };
readonly natural: { readonly natural: boolean };
readonly requiredFirst: { readonly requiredFirst: boolean };
}
interface RuleMetaData<MessageIds extends string> {
readonly type: 'suggestion';
readonly docs: {
readonly description: string;
readonly recommended: 'warn';
};
readonly messages: Record<MessageIds, string>;
readonly fixable: 'code';
readonly schema: JSONSchema4[];
}
type ESLintRule<MessageIds extends string, Options extends readonly unknown[]> =
RuleModule<MessageIds, Options, RuleListener>;