🔧 Error Fixes
· 1 min read

TypeScript: Type X Is Not Assignable to Type Y — How to Fix It


Type 'string' is not assignable to type 'number'

TypeScript caught a type mismatch.

Fix 1: Fix the type

// ❌ Wrong type
let count: number = "5";

// ✅ Correct type
let count: number = 5;

// ✅ Or parse it
let count: number = parseInt("5");

Fix 2: Update the type definition

// Maybe the type is too narrow
interface Config {
  port: number;
}

// ✅ Allow both
interface Config {
  port: number | string;
}

Fix 3: Use type assertion (last resort)

const value = someFunction() as string;

⚠️ Only use this when you’re sure about the type.