TypeScript plugin for ESLint providing comprehensive lint rules for TypeScript codebases
—
Rules focused on code style, naming conventions, and formatting preferences for TypeScript code. These rules help maintain consistent code appearance and readability across projects.
Rules that enforce consistent naming patterns for various code elements.
/**
* Enforce naming conventions for variables, functions, classes, etc.
*/
"naming-convention": RuleModule;Usage Examples:
// Configuration example for naming-convention
{
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "variable",
"format": ["camelCase", "UPPER_CASE"]
},
{
"selector": "function",
"format": ["camelCase"]
},
{
"selector": "typeLike",
"format": ["PascalCase"]
},
{
"selector": "interface",
"format": ["PascalCase"],
"prefix": ["I"]
}
]
}
// ❌ Bad
const user_name = 'john'; // Should be camelCase
function Process_Data() {} // Should be camelCase
interface userInterface {} // Should be PascalCase
// ✅ Good
const userName = 'john';
const MAX_RETRIES = 3;
function processData() {}
interface IUserInterface {}Rules that enforce consistent styling for arrays and objects.
/**
* Require consistently using either T[] or Array<T> for arrays
*/
"array-type": RuleModule;
/**
* Enforce consistent indexed object style
*/
"consistent-indexed-object-style": RuleModule;
/**
* Enforce consistent class literal property style
*/
"class-literal-property-style": RuleModule;
/**
* Disallow Array constructors
*/
"no-array-constructor": RuleModule;Usage Examples:
// ❌ Bad - array-type (with array-simple preference)
const users: Array<User> = []; // Should use T[]
const matrix: (number | string)[][] = []; // Complex type, Array<> ok
// ✅ Good
const users: User[] = [];
const matrix: Array<Array<number | string>> = [];
// ❌ Bad - class-literal-property-style (with fields preference)
class User {
get name(): string { return this._name; } // Should be field
set name(value: string) { this._name = value; }
}
// ✅ Good
class User {
public name: string;
}Rules that govern how type assertions and annotations are formatted.
/**
* Enforce consistent type assertions
*/
"consistent-type-assertions": RuleModule;
/**
* Enforce consistent generic constructors
*/
"consistent-generic-constructors": RuleModule;
/**
* Enforce consistent method signature style
*/
"method-signature-style": RuleModule;
/**
* Enforce consistent type definitions as interface or type
*/
"consistent-type-definitions": RuleModule;
/**
* Enforce non-nullable type assertion style
*/
"non-nullable-type-assertion-style": RuleModule;Usage Examples:
// ❌ Bad - consistent-type-assertions (with as preference)
const user = <User>data; // Should use 'as'
// ✅ Good
const user = data as User;
// ❌ Bad - method-signature-style (with property preference)
interface Calculator {
add(a: number, b: number): number; // Should be property style
}
// ✅ Good
interface Calculator {
add: (a: number, b: number) => number;
}
// ❌ Bad - consistent-generic-constructors (with constructor preference)
const users = new Map<string, User>(); // Should use constructor
// ✅ Good
const users: Map<string, User> = new Map();Rules that enforce consistent ordering of class members, object properties, and parameters.
/**
* Require consistent member declaration ordering
*/
"member-ordering": RuleModule;
/**
* Enforce default parameters to be last
*/
"default-param-last": RuleModule;
/**
* Require getter and setter pairs to be adjacent
*/
"related-getter-setter-pairs": RuleModule;
/**
* Enforce consistent sorting of type constituents
*/
"sort-type-constituents": RuleModule;Usage Examples:
// ❌ Bad - member-ordering (with fields-first preference)
class User {
public getName(): string { return this.name; } // Method before field
public name: string;
private constructor(name: string) { this.name = name; }
}
// ✅ Good
class User {
public name: string;
private constructor(name: string) {
this.name = name;
}
public getName(): string {
return this.name;
}
}
// ❌ Bad - sort-type-constituents
type Status = 'pending' | 'active' | 'inactive' | 'archived'; // Not sorted
// ✅ Good
type Status = 'active' | 'archived' | 'inactive' | 'pending'; // Alphabetically sortedRules that control code formatting, spacing, and layout.
/**
* Enforce dot notation whenever possible
*/
"dot-notation": RuleModule;
/**
* Require or disallow trailing commas
*/
"comma-dangle": RuleModule;
/**
* Enforce consistent spacing around keywords
*/
"keyword-spacing": RuleModule;
/**
* Enforce consistent spacing before and after commas
*/
"comma-spacing": RuleModule;
/**
* Enforce consistent spacing inside braces
*/
"object-curly-spacing": RuleModule;
/**
* Enforce consistent spacing around semicolons
*/
"semi-spacing": RuleModule;Usage Examples:
// ❌ Bad - dot-notation
const value = obj['property']; // Should use dot notation
// ✅ Good
const value = obj.property;
// ❌ Bad - comma-dangle (with always-multiline preference)
const config = {
api: '/api/v1',
timeout: 5000 // Missing trailing comma
};
// ✅ Good
const config = {
api: '/api/v1',
timeout: 5000,
};Rules that govern how literals and expressions are formatted.
/**
* Disallow magic numbers
*/
"no-magic-numbers": RuleModule;
/**
* Require literal enum members
*/
"prefer-literal-enum-member": RuleModule;
/**
* Prefer initializing enum members
*/
"prefer-enum-initializers": RuleModule;
/**
* Prefer as const over literal type
*/
"prefer-as-const": RuleModule;
/**
* Disallow unnecessary template expressions
*/
"no-unnecessary-template-expression": RuleModule;Usage Examples:
// ❌ Bad - no-magic-numbers
setTimeout(callback, 3000); // Magic number
// ✅ Good
const TIMEOUT_MS = 3000;
setTimeout(callback, TIMEOUT_MS);
// ❌ Bad - prefer-literal-enum-member
enum Status {
Active = getValue(), // Should be literal
Inactive = 'inactive'
}
// ✅ Good
enum Status {
Active = 'active',
Inactive = 'inactive'
}
// ❌ Bad - prefer-as-const
const colors = ['red', 'green', 'blue']; // Inferred as string[]
// ✅ Good
const colors = ['red', 'green', 'blue'] as const; // Readonly tupleRules that govern template literals and string formatting.
/**
* Enforce template literal expressions to be of string type
*/
"restrict-template-expressions": RuleModule;
/**
* Disallow unnecessary template expressions
*/
"no-unnecessary-template-expression": RuleModule;
/**
* Prefer string starts/ends with over regex or indexOf
*/
"prefer-string-starts-ends-with": RuleModule;
/**
* Prefer regexp exec over string match
*/
"prefer-regexp-exec": RuleModule;Usage Examples:
// ❌ Bad - restrict-template-expressions
const message = `User count: ${users}`; // Object in template
// ✅ Good
const message = `User count: ${users.length}`;
// ❌ Bad - prefer-string-starts-ends-with
if (str.indexOf('prefix') === 0) {} // Should use startsWith
// ✅ Good
if (str.startsWith('prefix')) {}
// ❌ Bad - no-unnecessary-template-expression
const greeting = `Hello`; // Should use regular string
// ✅ Good
const greeting = 'Hello';
const dynamicGreeting = `Hello, ${name}!`; // Template neededRules that govern comments and documentation formatting.
/**
* Disallow @ts-<directive> comments or require descriptions
*/
"ban-ts-comment": RuleModule;
/**
* Prefer @ts-expect-error over @ts-ignore
*/
"prefer-ts-expect-error": RuleModule;
/**
* Disallow // tslint:<rule-flag> comments
*/
"ban-tslint-comment": RuleModule;
/**
* Disallow triple slash reference directives except types
*/
"triple-slash-reference": RuleModule;Usage Examples:
// ❌ Bad - prefer-ts-expect-error
// @ts-ignore // No description
const result = problematicFunction();
// ✅ Good
// @ts-expect-error - Known issue with third-party library types
const result = problematicFunction();
// ❌ Bad - ban-tslint-comment
/* tslint:disable */ // Should use ESLint disable comment
// ✅ Good
/* eslint-disable @typescript-eslint/no-explicit-any */{
"rules": {
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "variable",
"format": ["camelCase", "UPPER_CASE"]
},
{
"selector": "function",
"format": ["camelCase"]
},
{
"selector": "typeLike",
"format": ["PascalCase"]
}
],
"@typescript-eslint/array-type": ["error", { "default": "array-simple" }],
"@typescript-eslint/consistent-type-assertions": ["error", { "assertionStyle": "as" }],
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/prefer-as-const": "error"
}
}{
"rules": {
"@typescript-eslint/no-magic-numbers": "off",
"@typescript-eslint/array-type": "off",
"@typescript-eslint/consistent-type-definitions": ["warn", "interface"],
"@typescript-eslint/dot-notation": "warn"
}
}interface NamingConventionRule {
selector: 'variable' | 'function' | 'parameter' | 'property' | 'method'
| 'accessor' | 'enumMember' | 'class' | 'interface' | 'typeAlias'
| 'enum' | 'typeParameter';
format: ('camelCase' | 'PascalCase' | 'snake_case' | 'UPPER_CASE')[];
prefix?: string[];
suffix?: string[];
leadingUnderscore?: 'forbid' | 'allow' | 'require';
trailingUnderscore?: 'forbid' | 'allow' | 'require';
filter?: string | { regex: string; match: boolean };
}
interface MemberOrderingRule {
default?: MemberType[];
classes?: MemberType[];
classExpressions?: MemberType[];
interfaces?: MemberType[];
typeLiterals?: MemberType[];
}
type MemberType =
| 'signature'
| 'field'
| 'constructor'
| 'method'
| 'accessor'
| ['field', { visibility?: 'public' | 'protected' | 'private' }]
| string;
interface ArrayTypeRule {
default: 'array' | 'generic' | 'array-simple';
readonly?: 'array' | 'generic' | 'array-simple';
}Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--eslint-plugin