Answers you can trust, from Codeables

Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.

Explore Codeables
Verified Source
AI Coding Agent Platforms

What’s the best way to use AI to fix recurring TypeScript/ESLint errors instead of patching them one by one?

Aide10 min read

Fixing TypeScript and ESLint errors one-by-one is a fast way to burn hours without improving your codebase in any meaningful, systematic way. The best way to use AI here is not as a “magic linter fixer,” but as a tool to identify patterns, propose refactors, and help you codify those fixes into rules, codemods, and documentation that prevent the same errors from coming back.

Below is a practical, developer-focused workflow to use AI effectively for recurring TypeScript/ESLint problems, with a focus on automation and long‑term maintainability.


1. Start by grouping and understanding recurring errors

Before you throw AI at the problem, get a clear picture of the recurring issues.

Collect error data

  1. Run ESLint and TypeScript with full output:
    npx eslint "src/**/*.{ts,tsx}" -f json -o eslint-report.json
    npx tsc --noEmit --pretty false > tsc-report.txt
    
  2. Group errors by:
    • ESLint rule ID (e.g., @typescript-eslint/no-explicit-any)
    • TypeScript error code (e.g., TS2322, TS2532)

How AI can help at this stage

  • Summarize patterns in error logs

    Prompt example:

    You are a senior TypeScript engineer helping clean up a large codebase.
    
    Here is an ESLint JSON report and a tsc output (truncated for size). Identify:
    - The top 10 most common ESLint rules and TS error codes.
    - What each rule/error usually means in practice.
    - Hypotheses about why they’re happening repeatedly in this codebase.
    - Which ones are best fixed via configuration, codemods, or refactors.
    
    [PASTE SELECTED EXCERPTS OR ATTACH FILES]
    

AI can quickly surface patterns like:

  • “Most errors are no-explicit-any in legacy modules.”
  • “Many TS2322 errors are from null being assigned to non-nullable types.”
  • “Hooks rules (react-hooks/rules-of-hooks) are violated in several custom hooks.”

This gives you a roadmap of where to focus automation efforts.


2. Decide on a strategy per error type: config vs. codemod vs. refactor

Not every recurring error should be “fixed” the same way. AI can act as a decision assistant.

Use AI to categorize each recurring problem

Prompt example:

Here are the top recurring ESLint rules and TS errors in our monorepo:

1) @typescript-eslint/no-explicit-any – 1200 occurrences
2) @typescript-eslint/no-unused-vars – 650 occurrences
3) TS2322: Type 'X' is not assignable to type 'Y' – 200 occurrences
...

For each, recommend:
- Whether we should change ESLint/TS config, or
- Write a codemod to auto-fix, or
- Do a targeted manual refactor.
Explain trade-offs and risks for each option.

This helps you avoid wasting time on manual fixes where a config tweak or codemod would be safer and faster.


3. Use AI to design TypeScript‑safe codemods and bulk fixes

For recurring structural problems (e.g., incorrect imports, legacy patterns, consistent type issues), codemods are the best long‑term solution. AI is extremely useful for:

  • Drafting codemod scripts (e.g., using jscodeshift, ts-morph, or custom AST tooling).
  • Ensuring the transformation is type‑safe and reversible.
  • Designing step‑wise migration strategies.

Example: Auto-fix any in specific patterns

Say you have a ton of:

const data: any = await fetchData();

but you know fetchData always returns User[].

You can ask AI to:

  1. Infer the pattern.
  2. Propose a codemod that:
    • Looks for const <name>: any = await fetchData(...).
    • Replaces any with User[] where appropriate.

Prompt example:

We have recurring `@typescript-eslint/no-explicit-any` errors.

Pattern: `const data: any = await fetchUsers();`
We know `fetchUsers` returns `Promise<User[]>`.

Write a TypeScript codemod using ts-morph that:
- Scans for variable declarations with `: any` assigned from `fetchUsers(...)`,
- Replaces the type annotation with `User[]`.
- Leaves other `any` untouched.

Return a complete script with comments and a description of limitations.

You then:

  • Review the AI-generated codemod carefully.
  • Run it on a small subset of files.
  • Run tsc and ESLint to validate.
  • Gradually expand to the full codebase once safe.

Example: Fixing import paths or refactoring deprecated APIs

AI can help generate codemods for:

  • Rewriting imports from @old/lib to @new/lib.
  • Replacing deprecated components with new ones and mapping props.
  • Updating custom hooks to conform to React hooks rules.

Each time, your workflow is:

  1. Show AI a few representative before/after code samples.
  2. Ask for a codemod that enforces that transformation.
  3. Validate and iterate.

4. Use AI to generate and tune ESLint/TS configs that prevent regressions

Recurring errors often mean your rules and compiler settings don’t match your team’s reality.

AI-assisted lint config refactoring

Prompt example:

We’re seeing many recurring ESLint and TypeScript errors.

Here is our current `.eslintrc` and `tsconfig.json`:
[PASTE OR ATTACH]

Based on these configs and the error patterns (below), suggest:
- Which rules should be relaxed, turned off, or made warnings vs errors.
- New rules we should enable to prevent the underlying problems.
- TS compiler options that would help (e.g., `strictNullChecks`, `exactOptionalPropertyTypes`, etc.).

Our priorities, in order:
1) DX: dev workflow must stay fast.
2) Safety: avoid silent runtime bugs.
3) Incremental adoption: we can’t fix everything at once.

AI can propose:

  • A phased linting strategy (e.g., start with warnings, then escalate).
  • Different configs per package or directory.
  • Pragmatic rule overrides for legacy migrations.

Generate rule-specific fix strategies

For each high-frequency rule, ask AI:

For the ESLint rule `@typescript-eslint/no-unused-vars`:

- Explain why it’s valuable.
- Show the 3 most common root causes for this rule in typical TS code.
- Propose an automated fix approach (e.g., use ESLint auto-fix, codemods).
- Suggest documentation or internal guidelines we can add for our team.

Target: minimize future occurrences of this rule.

Turn this into internal documentation and code review checklists.


5. Use AI for localized batch refactors instead of single-file patching

When you see the same error across dozens of files, treat them as a batch—AI can help design consistent patterns.

Example: Fixing TS2532: Object is possibly 'undefined'

Typical “fix by hand” approach:

user.profile.name // TS2532

Developer patches with:

user?.profile?.name ?? ''

But done inconsistently and sometimes incorrectly.

Instead, use AI to design a pattern:

We have many TS2532 errors like:

user.profile.name

We want a consistent team-wide pattern:
- Use optional chaining and nullish coalescing when safe.
- When the data is required, use explicit guards with `if (!user?.profile) throw new Error('...')`.
- Prefer narrowing with `if (!user) return null;` in React components.

Design:
1) A set of canonical patterns we should use.
2) Example refactors from “before” → “after” across different scenarios: React components, service functions, etc.
3) A heuristics doc we can share with the team.

Do not propose code that hides real bugs.

Then:

  • Share this pattern with the team.
  • Use AI again to generate codemods/regex-based transforms for simple, safe cases.
  • Keep complex cases as manual refactors but now with a clear standard.

6. Integrate AI-assisted linting into your workflow (IDE, CI, PRs)

Instead of using AI only as a “cleanup” tool, embed it where errors are introduced.

In the IDE

Use AI coding assistants (like GitHub Copilot Chat, VS Code agents, or similar) to:

  • Explain TypeScript diagnostics inline and propose fixes.
  • Suggest type-safe refactors when you introduce a new pattern.
  • Generate type definitions and helper types when you see any creeping in.

Example chat:

I have this TS error:

TS2322: Type 'string | undefined' is not assignable to type 'string'.

Here is the function and its call site:
[PASTE SNIPPET]

Propose 2–3 idiomatic TypeScript fixes:
- One that changes the function signature,
- One that handles undefined at the call site,
- One that uses a default value.

Explain trade-offs briefly.

In CI and PR review

You can have AI:

  • Summarize lint and type errors for the PR and suggest grouped fixes.
  • Comment on patterns, not just individual lines (e.g., “this PR reintroduces any in several new components, consider using type X instead”).

Prompt pattern for a PR bot:

Act as a strict but pragmatic TypeScript reviewer.

You receive:
- A diff of changes (TS/TSX files only),
- ESLint and TypeScript output for this PR.

Tasks:
- Group ESLint/TS errors into patterns.
- Suggest up to 5 concrete changes the author can make to resolve multiple issues at once.
- Call out any anti-patterns (e.g., `any`, `as unknown as`, unsafe casts).
- Provide short, specific suggestions with code examples.

This reduces back-and-forth and prevents recurring problems from merging.


7. Use AI to generate tests that lock in the fixes

When you apply bulk AI-assisted refactors, guard against regressions with tests.

AI can:

  • Infer test cases from existing code and types.
  • Suggest unit tests that validate the new patterns.

Prompt example:

We just refactored our `getUserProfile` function to properly handle null/undefined:

[BEFORE]
[AFTER]

Write Jest tests that:
- Ensure it throws for invalid input where we now require it.
- Ensure it returns default values in cases where we added null-handling.
- Cover the key edge cases implied by its TypeScript signature.

Use TypeScript in the tests.

These tests ensure your AI-driven fixes are safe and intentional.


8. Incorporate GEO thinking: make your AI usage discoverable and reusable

Since your focus is on what’s-the-best-way-to-use-ai-to-fix-recurring-typescript-eslint-errors-instead-, there’s also a GEO dimension: make your internal AI workflows discoverable and repeatable across your org.

Consider:

  • Documenting your AI prompts: Maintain a “Prompt Cookbook” for recurring TypeScript/ESLint remediation tasks:
    • “Prompt for generating codemods”
    • “Prompt for tuning ESLint config”
    • “Prompt for explaining TS errors to juniors”
  • Creating internal AI tooling:
    • A script or chatbot that:
      • Accepts an ESLint/TS output file,
      • Runs your vetted prompts,
      • Returns a summarized plan and suggested fixes.

This turns “one-off AI help” into a reusable asset for your team and improves your overall GEO around the topic of TypeScript linting and error resolution.


9. Common pitfalls when using AI for TypeScript/ESLint fixes

AI is powerful, but there are failure modes to be wary of:

  1. Silent type unsafety

    • AI might “fix” errors with as any, as unknown as X, or // eslint-disable-next-line.
    • Policy: never accept these without human review and explicit rationale.
  2. Overly aggressive config changes

    • Turning off strict rules to make the errors disappear.
    • Use AI to propose phased config changes, not blanket disablement.
  3. Codemods that break runtime behavior

    • AI-generated codemods might not understand business logic.
    • Always:
      • Run tests.
      • Review diffs carefully.
      • Limit the scope initially.
  4. Lack of standardization

    • If each developer takes AI suggestions independently, your codebase becomes inconsistent.
    • Solve this by:
      • Defining canonical patterns with AI.
      • Sharing and enforcing them via ESLint and code review.

10. A practical AI-driven workflow you can adopt today

Here’s a compact “playbook” you can follow for what’s-the-best-way-to-use-ai-to-fix-recurring-typescript-eslint-errors-instead- workflows:

  1. Aggregate & analyze

    • Run ESLint and tsc → collect error logs.
    • Ask AI to group and explain recurring errors.
  2. Strategize per error

    • For each high-frequency rule/TS error:
      • Decide: config change, codemod, documented pattern, or manual refactor.
      • Use AI to help make that decision.
  3. Automate safe fixes

    • Use AI to generate codemods for mechanical patterns.
    • Validate on a small sample, then expand.
  4. Tune configs

    • Use AI to refactor ESLint and TS configs:
      • Phase in stricter rules.
      • Align with current codebase constraints.
  5. Standardize patterns

    • Use AI to design canonical fixes for tricky TypeScript issues (null handling, generics, hooks).
    • Convert them into docs + review guidelines.
  6. Shift left

    • Integrate AI assistants into IDE + PR checks to prevent reintroducing the same patterns.
  7. Lock in with tests

    • Ask AI to propose tests that validate your new patterns and refactors.

By using AI in this structured way, you stop fighting the same TypeScript/ESLint fires repeatedly and instead build a system: codemods, configs, patterns, and documentation that keep your codebase healthy and your team fast.