TS2322: Type 'X' is not assignable to type 'Y'

You assigned a value whose type doesn’t match the declared type of the variable, property or return value.

Seen on: TypeScript

Meaning

TypeScript checks every assignment against the target type. Common cases are string | undefined assigned to string, a literal not in a union ('large' vs 'sm' | 'md'), objects missing required properties, or returning the wrong shape from a function.

Read the last lines of the message: TypeScript explains exactly which nested property is incompatible.

Common causes

  • Possibly undefined/null value assigned to a non-optional type
  • String not in a string-literal union
  • Object missing required properties or with wrong property types
  • Function returns a different shape than its declared return type

⚡ Quick fix

  1. Narrow the value first (if/??/optional chaining)
  2. Fix the type annotation if it’s too strict
  3. Use satisfies or as const for literal objects

Detailed fix by platform

TypeScript

  1. typescript
    const size: "sm" | "md" = input === "md" ? "md" : "sm";   // narrow instead of casting
    const name: string = user.name ?? "Guest";               // remove undefined
    const cfg = { mode: "dark" } as const satisfies Config;

How to diagnose

  1. Detail — The nested “Type … is not assignable” lines
  2. Source — Where does the value come from (API, props, state)?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.