TS2322
TypeScriptERRORNotableType SystemHIGH confidence

Type is not assignable to type

What this means

TypeScript found a type mismatch. The value being assigned does not satisfy the type constraint of the target.

Why it happens
  1. 1Assigning a string to a variable declared as number.
  2. 2Passing an object missing required fields to a typed function.
  3. 3Returning the wrong type from a typed function.
How to reproduce

A variable declared as number receives a string value.

trigger — this will error
trigger — this will error
let count: number = "hello";

expected output

error TS2322: Type 'string' is not assignable to type 'number'.

Fix 1

Use the correct type value

WHEN When the value is simply the wrong type

Use the correct type value
let count: number = 42;

Why this works

Using a number literal satisfies the number type constraint.

Fix 2

Widen the type declaration

WHEN When the variable genuinely needs to hold multiple types

Widen the type declaration
let count: number | string = "hello";

Why this works

A union type explicitly allows the variable to hold values of either type.

What not to do

Cast with any to silence the error

any disables type checking entirely and defeats the purpose of TypeScript.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

TypeScript Handbook

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors