or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdinterface-rule.mdstring-enum-rule.md
tile.json

tessl/npm-eslint-plugin-typescript-sort-keys

ESLint plugin for TypeScript that enforces consistent sorting of interface keys and string enum members

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/eslint-plugin-typescript-sort-keys@3.3.x

To install, run

npx @tessl/cli install tessl/npm-eslint-plugin-typescript-sort-keys@3.3.0

index.mddocs/

ESLint Plugin TypeScript Sort Keys

ESLint 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.

Package Information

  • Package Name: eslint-plugin-typescript-sort-keys
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev eslint-plugin-typescript-sort-keys
  • Peer Dependencies:
    • eslint: ^7 || ^8
    • @typescript-eslint/parser: >=6
    • typescript: ^3 || ^4 || ^5
  • Node Version: >= 16

Core Imports

Plugin 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"
  }
}

Basic Usage

// 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'
}

Architecture

The plugin consists of:

  • Main Plugin Export: Configuration object containing rules and preset configurations
  • Rule Implementations: Two ESLint rules for interface and string enum sorting
  • Recommended Configuration: Preset ESLint configuration with both rules enabled
  • Sorting Options: Configurable sorting behavior (case sensitivity, natural order, required-first)

Capabilities

Main Plugin Export

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;

Interface Sorting Rule

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';

Interface Rule

String Enum Sorting Rule

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;
  }>?
];

String Enum Rule

Recommended Configuration

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'
  }
};

Common Types

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>;