Error Encyclopedia

TypeScript Type Not Assignable Error

Fix 'Type X is not assignable to type Y' errors in TypeScript. Learn how to debug type mismatches, use proper typing, and leverage type inference.

What Does This Error Mean?

The 'Type X is not assignable to type Y' error means you are trying to use a value of one type where a different type is expected. TypeScript's type checker ensures type safety by catching these mismatches at compile time.

Common Causes

1

Function parameter expects one type but receives another

2

Object property is missing or has the wrong type

3

Null or undefined passed to a non-nullable parameter

4

Generic type parameter does not satisfy constraints

5

Return type of function does not match declared return type

6

Type union does not include the value being assigned

How to Fix It

Check the expected vs actual type

Hover over the error in your IDE to see the exact type mismatch. The error message shows both the expected type and the type you're providing.

// Error: Type "string" is not assignable to type "number"
const age: number = "25" // ❌
const age: number = 25    // ✅

Use type guards

Use type guards to narrow types before using them.

function process(value: string | number) {
  // ❌ Error: toUpperCase() not on number
  // return value.toUpperCase()
  
  // ✅ Type guard
  if (typeof value === "string") return value.toUpperCase()
  return value.toFixed(2)
}

Fix missing properties

Ensure objects include all required properties from the type or interface.

interface User {
  id: number
  name: string
  email: string
}

// ❌ Missing email property
const user: User = { id: 1, name: "John" }

// ✅ All required properties
const user: User = { id: 1, name: "John", email: "john@example.com" }

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category:

Frequently Asked Questions

What does 'is not assignable to type never' mean?

This usually means TypeScript has narrowed a type to 'never' (no possible values). This happens when all branches of a union are exhausted or when you have contradictory type guards.

Should I use 'as' (type assertion) to fix type errors?

Type assertions (`as`) bypass TypeScript's type checking. Use them sparingly. Always prefer proper typing, type guards, or type narrowing over assertions.