Type is not assignable to type
TypeScript found a type mismatch. The value being assigned does not satisfy the type constraint of the target.
- 1Assigning a string to a variable declared as number.
- 2Passing an object missing required fields to a typed function.
- 3Returning the wrong type from a typed function.
A variable declared as number receives a string value.
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
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
let count: number | string = "hello";
Why this works
A union type explicitly allows the variable to hold values of either type.
✕ Cast with any to silence the error
any disables type checking entirely and defeats the purpose of TypeScript.
microsoft/TypeScript src/compiler/diagnosticMessages.json
TypeScript Handbook ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev