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 returnany/unknown
tsx
3. Hooks with Any Types
Detects hooks that:
- Accept
Promise<any>in parameters - Return
anytype
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
unknownwith 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)