CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typescript-eslint--eslint-plugin

TypeScript plugin for ESLint providing comprehensive lint rules for TypeScript codebases

Pending
Overview
Eval results
Files

type-safety-rules.mddocs/

Type Safety Rules

Rules focused on TypeScript type safety, preventing common type-related errors and enforcing proper type usage. These rules leverage TypeScript's type system to catch potential runtime errors at build time.

Capabilities

Unsafe Type Operations

Rules that prevent operations on any types and other unsafe type interactions.

/**
 * Disallow assigning any values to variables and properties
 */
"no-unsafe-assignment": RuleModule;

/**
 * Disallow calling any typed value
 */
"no-unsafe-call": RuleModule;

/**
 * Disallow member access on any typed variables
 */
"no-unsafe-member-access": RuleModule;

/**
 * Disallow returning any from a function
 */
"no-unsafe-return": RuleModule;

/**
 * Disallow calling a function with any type argument
 */
"no-unsafe-argument": RuleModule;

/**
 * Disallow unsafe type assertions using any
 */
"no-unsafe-type-assertion": RuleModule;

/**
 * Disallow unsafe function types
 */
"no-unsafe-function-type": RuleModule;

/**
 * Disallow unsafe enum comparison
 */
"no-unsafe-enum-comparison": RuleModule;

/**
 * Disallow unsafe declaration merging
 */
"no-unsafe-declaration-merging": RuleModule;

/**
 * Disallow unsafe unary minus operator
 */
"no-unsafe-unary-minus": RuleModule;

/**
 * Require .toString() to only be called on objects which provide useful information when stringified
 */
"no-base-to-string": RuleModule;

/**
 * Disallow the any type
 */
"no-explicit-any": RuleModule;

/**
 * Enforce valid definition of new and constructor
 */
"no-misused-new": RuleModule;

Usage Examples:

// ❌ Bad - no-unsafe-assignment
let foo: number;
foo = bar as any; // Error: Unsafe assignment

// ✅ Good
let foo: number;
foo = bar as number; // Type assertion with proper type

// ❌ Bad - no-unsafe-call
declare const fn: any;
fn(); // Error: Unsafe call

// ✅ Good
declare const fn: () => void;
fn(); // Safe call with proper typing

Promise and Async Safety

Rules that ensure proper handling of Promises and async operations.

/**
 * Require Promise-like values to be handled appropriately
 */
"no-floating-promises": RuleModule;

/**
 * Disallow awaiting a value that is not a Thenable
 */
"await-thenable": RuleModule;

/**
 * Disallow Promises in places not designed to handle them
 */
"no-misused-promises": RuleModule;

/**
 * Enforce consistent returning of awaited values
 */
"return-await": RuleModule;

/**
 * Require async functions to return a Promise
 */
"promise-function-async": RuleModule;

Usage Examples:

// ❌ Bad - no-floating-promises
fetchData(); // Error: Promise not handled

// ✅ Good
await fetchData(); // Promise properly awaited
fetchData().catch(console.error); // Promise error handled

// ❌ Bad - await-thenable
await 42; // Error: Awaiting non-thenable

// ✅ Good
await Promise.resolve(42); // Awaiting actual Promise

// ❌ Bad - no-misused-promises
if (fetchData()) { // Error: Promise in boolean context
  // ...
}

// ✅ Good
const result = await fetchData();
if (result) {
  // ...
}

Type Assertion Safety

Rules that govern the safe use of type assertions and non-null assertions.

/**
 * Disallow non-null assertions using the ! postfix operator
 */
"no-non-null-assertion": RuleModule;

/**
 * Disallow extra non-null assertions
 */
"no-extra-non-null-assertion": RuleModule;

/**
 * Disallow non-null assertion in left operand of nullish coalescing
 */
"no-non-null-asserted-nullish-coalescing": RuleModule;

/**
 * Disallow non-null assertion in left operand of optional chaining
 */
"no-non-null-asserted-optional-chain": RuleModule;

/**
 * Disallow confusing non-null assertion-like expressions
 */
"no-confusing-non-null-assertion": RuleModule;

/**
 * Enforce consistent type assertions
 */
"consistent-type-assertions": RuleModule;

/**
 * Disallow unnecessary type assertions
 */
"no-unnecessary-type-assertion": RuleModule;

Usage Examples:

// ❌ Bad - no-non-null-assertion
const user = getUser()!; // Error: Non-null assertion

// ✅ Good
const user = getUser();
if (user) {
  // Use user safely
}

// ❌ Bad - no-extra-non-null-assertion
const value = foo!!!.bar; // Error: Extra non-null assertions

// ✅ Good
const value = foo!.bar; // Single assertion if necessary

// ❌ Bad - no-non-null-asserted-nullish-coalescing
const value = foo! ?? 'default'; // Error: Non-null assertion with nullish coalescing

// ✅ Good
const value = foo ?? 'default'; // Just use nullish coalescing

Type Checking and Validation

Rules that enforce proper type checking and validation patterns.

/**
 * Disallow unnecessary conditionals
 */
"no-unnecessary-condition": RuleModule;

/**
 * Disallow unnecessary boolean literal compare
 */
"no-unnecessary-boolean-literal-compare": RuleModule;

/**
 * Disallow unnecessary type arguments
 */
"no-unnecessary-type-arguments": RuleModule;

/**
 * Disallow unnecessary type constraints
 */
"no-unnecessary-type-constraint": RuleModule;

/**
 * Disallow unnecessary type parameters
 */
"no-unnecessary-type-parameters": RuleModule;

/**
 * Disallow unnecessary type conversion
 */
"no-unnecessary-type-conversion": RuleModule;

/**
 * Enforce template literal expressions to be of string type
 */
"restrict-template-expressions": RuleModule;

/**
 * Require both operands of addition to have type number or string
 */
"restrict-plus-operands": RuleModule;

/**
 * Enforce strict boolean expressions
 */
"strict-boolean-expressions": RuleModule;

/**
 * Require switch-case statements to be exhaustive with union type
 */
"switch-exhaustiveness-check": RuleModule;

Usage Examples:

// ❌ Bad - no-unnecessary-condition
const str: string = 'hello';
if (str) { // Error: Always truthy
  // ...
}

// ✅ Good
if (str.length > 0) { // Meaningful condition
  // ...
}

// ❌ Bad - restrict-plus-operands
const result = value + {}; // Error: Object in addition

// ✅ Good
const result = value + String(obj); // Explicit conversion

// ❌ Bad - strict-boolean-expressions
if (value) { // Error: Not explicitly boolean
  // ...
}

// ✅ Good
if (value !== null && value !== undefined) {
  // ...
}

// ❌ Bad - switch-exhaustiveness-check
type Color = 'red' | 'green' | 'blue';
function handleColor(color: Color) {
  switch (color) {
    case 'red':
      return '#ff0000';
    case 'green':
      return '#00ff00';
    // Error: Missing case for 'blue'
  }
}

// ✅ Good
function handleColor(color: Color) {
  switch (color) {
    case 'red':
      return '#ff0000';
    case 'green':
      return '#00ff00';
    case 'blue':
      return '#0000ff';
    default:
      return assertNever(color);
  }
}

Type Definition Safety

Rules that ensure safe usage of TypeScript type definitions and declarations.

/**
 * Disallow duplicate enum values
 */
"no-duplicate-enum-values": RuleModule;

/**
 * Disallow duplicate type constituents
 */
"no-duplicate-type-constituents": RuleModule;

/**
 * Disallow redundant type constituents
 */
"no-redundant-type-constituents": RuleModule;

/**
 * Disallow empty interfaces
 */
"no-empty-interface": RuleModule;

/**
 * Disallow empty object types
 */
"no-empty-object-type": RuleModule;

/**
 * Disallow invalid void type usage
 */
"no-invalid-void-type": RuleModule;

/**
 * Disallow wrapper object types
 */
"no-wrapper-object-types": RuleModule;

/**
 * Disallow mixed enums
 */
"no-mixed-enums": RuleModule;

Usage Examples:

// ❌ Bad - no-duplicate-enum-values
enum Direction {
  Up = 1,
  Down = 1, // Error: Duplicate value
  Left = 2,
  Right = 3
}

// ✅ Good
enum Direction {
  Up = 1,
  Down = 2,
  Left = 3,
  Right = 4
}

// ❌ Bad - no-empty-interface
interface EmptyInterface {} // Error: Empty interface

// ✅ Good
interface UserInterface {
  name: string;
  email: string;
}

// ❌ Bad - no-invalid-void-type
let value: void = undefined; // Error: Invalid void usage

// ✅ Good
function doSomething(): void {
  // Function returning void
}

Array and Iteration Safety

Rules that ensure safe operations on arrays and iterables.

/**
 * Disallow iterating over an array with a for-in loop
 */
"no-for-in-array": RuleModule;

/**
 * Disallow spreading non-iterable types
 */
"no-misused-spread": RuleModule;

/**
 * Require Array.from() over spreading non-arrays
 */
"prefer-for-of": RuleModule;

/**
 * Require array-like objects to be converted with Array.from()
 */
"prefer-includes": RuleModule;

/**
 * Require using the built-in comparison function in Array.sort()
 */
"require-array-sort-compare": RuleModule;

Usage Examples:

// ❌ Bad - no-for-in-array
const arr = [1, 2, 3];
for (const key in arr) { // Error: for-in on array
  console.log(arr[key]);
}

// ✅ Good
for (const item of arr) {
  console.log(item);
}

// ❌ Bad - no-misused-spread
const notIterable = { a: 1, b: 2 };
const arr = [...notIterable]; // Error: Spreading non-iterable

// ✅ Good
const arr = Object.values(notIterable);

Types

interface RuleModule {
  meta: {
    type: "problem" | "suggestion" | "layout";
    docs: {
      description: string;
      recommended?: boolean | string;
      requiresTypeChecking?: boolean;
    };
    messages: Record<string, string>;
    schema: JSONSchema;
  };
  create(context: RuleContext): RuleListener;
}

interface RuleContext {
  id: string;
  options: unknown[];
  settings: Record<string, unknown>;
  parserOptions: ParserOptions;
  getSourceCode(): SourceCode;
  report(descriptor: ReportDescriptor): void;
}

interface RuleListener {
  [nodeType: string]: (node: Node) => void;
}

Install with Tessl CLI

npx tessl i tessl/npm-typescript-eslint--eslint-plugin

docs

best-practices-rules.md

code-quality-rules.md

index.md

plugin-configuration.md

style-formatting-rules.md

type-safety-rules.md

tile.json