Ad Space — Top Banner

TS2322

TypeScript Programming Language

Severity: Minor

What Does This Error Mean?

TypeScript error TS2322 means you tried to assign a value to a variable or return a value from a function, but the types do not match. For example, assigning a string to a variable that was declared as number will cause TS2322. TypeScript checks assignments to ensure type safety — it prevents you from putting the wrong kind of value somewhere. Fix it by correcting the value's type, updating the variable's declared type, or converting the value.

Affected Models

  • TypeScript 1.0 and later
  • All TypeScript versions
  • tsc compiler, ts-node, webpack, Vite, Next.js

Common Causes

  • Assigning a value of the wrong type to a typed variable: let x: number = 'hello'
  • Returning a value from a function that does not match the declared return type
  • Assigning an object that is missing required properties to a typed variable
  • TypeScript inferred a variable as one type but you tried to reassign it with a different type
  • Assigning null or undefined to a variable that does not include null in its type

How to Fix It

  1. Read the error message carefully: 'Type X is not assignable to type Y'. Identify what X is and what Y is.

    X is what you are trying to assign. Y is what was expected. The fix is usually to change X to match Y.

  2. If the types are incompatible, convert the value. For example, to assign a number to a string variable: let s: string = String(myNumber);

    TypeScript does not convert types automatically. Every conversion must be written explicitly.

  3. If a function returns the wrong type, update the function's return value to match the declared return type.

    Check the return type annotation after the function name: function myFunc(): string { ... }. Make sure the function actually returns a string.

  4. If the error says 'Type null is not assignable', either add null to the variable's type (string | null) or add a null check before the assignment.

    If you have strictNullChecks enabled in tsconfig.json (the default in strict mode), null and undefined are not valid unless explicitly included in the type.

  5. If you are assigning an object literal and a property is missing, add the missing property or mark it optional in the interface with a ?.

    Example: interface MyType { name: string; age?: number; } — the ? makes age optional so objects without it are valid.

When to Call a Professional

TS2322 is always a compile-time error. The compiler shows you the actual type and the expected type. This error exists to prevent runtime bugs caused by wrong types reaching the wrong places. In most cases the fix is straightforward once you understand both types.

Frequently Asked Questions

Why does TypeScript not just allow assigning any value to any variable?

TypeScript's type system exists to catch bugs early. If you assign a string to a number variable, any math done with that variable later will fail at runtime. By catching the mismatch at compile time, TypeScript stops the bug before it reaches users. The strictness is intentional — it is the whole point of using TypeScript over plain JavaScript.

I keep getting TS2322 with null — how do I fix it cleanly?

Enable strict mode in tsconfig.json (if not already) and handle null explicitly. Change your type to include null where it is valid: let user: User | null = null; Use optional chaining when accessing properties: user?.name This is the TypeScript-recommended approach for nullable values.

How is TS2322 different from TS2345?

TS2322 is a type error at an assignment: let x: Type = wrongTypeValue. TS2345 is a type error at a function call: myFunction(wrongTypeValue). They are essentially the same problem in different contexts. The fix strategies are the same.