Skip to main content

no-misused-generics

Disallow generic type parameters that cannot be inferred or that do not enforce a meaningful type relationship.

Targeted pattern scopeโ€‹

โš ๏ธ This rule requires type information to run. Configure type-aware linting (parserOptions.project or projectService) before enabling it.

This rule checks TypeScript signature declarations that declare generic type parameters, including:

  • function declarations and expressions,
  • arrow functions,
  • methods,
  • call/construct signatures,
  • function and constructor type signatures.

What this rule reportsโ€‹

This rule reports type parameters when either of the following is true:

  • the type parameter cannot be inferred from any function parameter (cannotInfer), or
  • the type parameter is inferable but does not enforce a meaningful relationship and can be replaced by unknown (or its explicit constraint) (canReplace).

Why this rule existsโ€‹

Type parameters should encode real relationships between values and types. If a type parameter cannot be inferred, callers must always provide it manually. If it does not constrain behavior, it often adds complexity without extra safety.

โŒ Incorrectโ€‹

declare function get<T>(): T;
declare function take<T>(value: T): void;

โœ… Correctโ€‹

declare function identity<T>(value: T): T;
declare function compare<T>(left: T, right: T): boolean;
declare function compare<T, U extends T>(left: T, right: U): boolean;

Behavior and migration notesโ€‹

This rule has no options.

When this rule reports canReplace, replacing the type parameter with unknown (or with the explicit constraint) usually preserves intent while reducing generic noise.

Additional examplesโ€‹

function wrap<T extends string>(value: T): void {
console.log(value);
}
// โŒ reported with `canReplace`: `T` can be replaced with `string`

function pair<T>(left: T, right: T): [T, T] {
return [left, right];
}
// โœ… valid: `T` meaningfully relates parameters and return type

ESLint flat config exampleโ€‹

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/no-misused-generics": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your project intentionally keeps explicit, documentation-only type parameters even when they are not inferable or enforceable.

Package documentationโ€‹

Rule catalog ID: R031

Further readingโ€‹

Adoption resourcesโ€‹

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.