Skip to main content

TypeScript analyzer

TypeScript Generics Usage

Flags generic-type arity, unused and single-purpose type parameters, and constraints that constrain nothing.

technical-debt ts-generics

TypeScript Generics Usage

Flags generic-type arity, unused and single-purpose type parameters, and constraints that constrain nothing.

Generic type parameters are TypeScript's main tool for safe abstraction, but they have costs: each parameter is a degree of freedom callers must understand and satisfy. High arity (5+ type parameters), unused parameters, single-use parameters, and overly broad constraints all signal that a generic is overspecified for its actual use sites.

References are resolved by declaration, not by name: a mapped type's own key or a nested generic that re-declares a name is not a use of the outer parameter. A single use is reported only on a function or method with a declared return type, and not when the parameter is a type argument of another generic (props: TableProps<T>), where it ties that type's members together. extends object is a real constraint and is not reported.

Severity guide

info
Generic usage is unusual but acceptable; review during a refactor.
warning
Generic shape will confuse callers or hide bugs; refactor recommended.
critical
Not currently emitted by this analysis.

Remediation

Reduce type-parameter arity, remove unused parameters, and narrow constraints that constrain nothing.

Reduce type-parameter arity, remove unused parameters, replace a single-purpose parameter with the type it stands for, and narrow {} / any constraints to the shape the code relies on.

Documentation