Common causes
- Assigning a string to a variable declared as a number, or vice versa
- A function's return value doesn't match its declared return type
- Passing a union type where only one specific member is accepted
- Assigning null or undefined to a non-nullable type with strict null checks enabled
How to fix it
- Update the value to match the expected type, or loosen the declared type to accept the actual value
- If the type annotation is wrong, fix it: change number to string if the value is always a string
- Use a type guard to narrow before assigning: if (typeof value === 'string') { ... }
- As a last resort, use a type assertion: value as TargetType — only when you're certain of the type
Example
const count: number = 'five';
// error TS2322: Type 'string' is not assignable to type 'number'.Assigning a string literal to a variable declared as number
Browse more errors
The Developer Hub covers 150+ errors across Git, npm, Node.js, Python, TypeScript, and Docker — with plain-English explanations and fix steps.
