Fix TypeScript Strict Mode Errors with Claude Code

The Problem

You enable "strict": true in tsconfig.json and suddenly your project has hundreds of errors:

src/api/users.ts:23:5 - error TS2322: Type 'string | undefined' is not assignable to type 'string'.
src/utils/format.ts:8:22 - error TS7006: Parameter 'item' implicitly has an 'any' type.
src/models/User.ts:12:3 - error TS2564: Property 'email' has no initializer.

Quick Fix

Ask Claude Code to fix strict mode errors file by file:

Run tsc --noEmit and show me the error count per file. Then fix each file
starting with the one that has the most errors. Use proper types, not any.

What strict: true Enables

FlagWhat it catches
strictNullChecksVariables that could be null or undefined used without checking
noImplicitAnyParameters and variables without type annotations
strictFunctionTypesContravariant function parameter checking
strictPropertyInitializationClass properties not initialized in constructor
useUnknownInCatchVariablesCatch variables typed as unknown instead of any

Step-by-Step Fix

Step 1: Enable strict mode incrementally

{
  "compilerOptions": {
    "strict": false,
    "strictNullChecks": true
  }
}

Step 2: Fix strictNullChecks errors

// Error: Object is possibly 'undefined'
function getUserName(user?: User): string {
  return user.name; // TS2532
}

// Fix with a guard
function getUserName(user?: User): string {
  if (!user) throw new Error('User is required');
  return user.name;
}

// Or with a default
function getUserName(user?: User): string {
  return user?.name ?? 'Anonymous';
}

Step 3: Fix noImplicitAny errors

// Error: Parameter 'item' implicitly has an 'any' type
function processItems(items) {
  return items.map(item => item.name);
}

// Fix: add type annotations
interface Item { name: string; value: number; }
function processItems(items: Item[]): string[] {
  return items.map(item => item.name);
}

Step 4: Fix strictPropertyInitialization

// Error: Property 'email' has no initializer
class User {
  name: string;  // TS2564
  email: string; // TS2564
}

// Fix: Initialize in constructor
class User {
  name: string;
  email: string;
  constructor(name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}

Step 5: Fix useUnknownInCatchVariables

// Error: Object is of type 'unknown'
try { await fetchData(); }
catch (error) { console.log(error.message); } // TS18046

// Fix: type guard
try { await fetchData(); }
catch (error) {
  if (error instanceof Error) {
    console.log(error.message);
  } else {
    console.log('Unknown error:', String(error));
  }
}

Prevention

Always start new projects with strict mode enabled. Add to your CLAUDE.md: strict mode is enabled, do not add @ts-ignore or type assertions. Use proper null guards, not non-null assertions. Every function parameter must have an explicit type. Prefer unknown over any for dynamic values.

Paste your error into our Error Diagnostic for an instant fix.

Master Claude Code

Get lifetime access to all ClaudHQ tools, advanced workflows, and production-grade templates.

Get Lifetime Access

Written by the ClaudHQ team ยท Expert Claude Code guides and tools