Aalexbit-codemod

hooks-missing-generics

Detects missing generics on hooks in TypeScript/TSX codebases.

code-miningreacthooksgenericstype-widening
Public
6 executions
0 stars
How to Use
Run this codemod on your codebase using one of the following commands

The easiest way to run this codemod without installing anything globally:

Documentation

Hooks Missing Generics Codemod

A JSSG codemod that detects missing generic type parameters on hooks in TypeScript/TSX codebases and adds TODO comments to flag type widening issues.

What It Detects

1. useState with Empty Literals

Detects useState([]) or useState({}) calls without generic type parameters:

tsx

2. Custom Hooks Wrapping Other Hooks

Detects custom hooks that:

  • Are named useX (following React hook naming convention)
  • Call other hooks inside (useState, useQuery, useMutation, etc.)
  • Don't declare generic type parameters
  • Accept Promise<any> or return any/unknown
tsx

3. Hooks with Any Types

Detects hooks that:

  • Accept Promise<any> in parameters
  • Return any type
tsx

Usage

Run the Codemod

bash

Test the Codemod

bash

Update Test Snapshots

bash

Output Format

For each detected issue, the codemod adds a TODO comment above the problematic code:

tsx

Rule ID

  • hooks.missing-generics - All findings use this rule identifier

Non-Goals

  • Does not require framework-specific knowledge beyond common hooks
  • Avoids false positives for hooks that intentionally return unknown with runtime validation
  • Does not auto-fix issues (only adds TODO comments)

Examples

See the tests/ directory for comprehensive test cases covering:

  • useState with empty arrays
  • useState with empty objects
  • Custom hooks wrapping hooks
  • Hooks with Promise<any>
  • Hooks returning any
  • Negative cases (should not be flagged)

Ready to contribute?

Build your own codemod and share it with the community.