TypeScriptType ErrorsError Fix2026 Updated

TypeScript: Type X Is Not Assignableto Type Y — Fixed [2026]

Also covers: type widening · as const · union types · type guards · function return types · interface mismatch

January 20267 min readTypeScript 5.x · React · Next.js

⚡ The Error

Error
Type 'string' is not assignable to type '"red" | "green" | "blue"'.
// OR:
Type '{ name: string; }' is not assignable to type 'User'.
  Property 'email' is missing in type '{ name: string; }'
  but required in type 'User'.
// OR:
Type 'string | undefined' is not assignable to type 'string'.

✅ Fastest Fix — check what the error is actually saying

Read the error from bottom to top
// "Property 'email' is missing" → add the email property
// "string | undefined not assignable to string" → handle the undefined case
// "string not assignable to 'red' | 'green'" → use as const or type guard

What Causes 'Type X Is Not Assignable to Type Y'

TypeScript's structural type system checks that values have the right shape before allowing assignment. This error fires when the source type has properties missing, extra incompatible properties, a different literal value, or is wider than the destination type accepts. Reading the full error message (especially the bottom lines about which specific property or value is wrong) almost always points directly to the fix.

1

Narrow the Type with a Type Guard

string | undefined not assignable to string
3 min

When TypeScript infers string | undefined or string | null, you must narrow to string before using it where only string is accepted.

Type narrowing patterns
// ❌ Error: Type 'string | undefined' is not assignable to type 'string'
function greet(name: string | undefined) {
  const upper = name.toUpperCase()  // name might be undefined
}

// ✅ Fix 1 — nullish check
function greet(name: string | undefined) {
  if (!name) return
  const upper = name.toUpperCase()  // TypeScript knows name is string here
}

// ✅ Fix 2 — nullish coalescing
function greet(name: string | undefined) {
  const display = name ?? "Guest"         // string — never undefined
  const upper = display.toUpperCase()
}

// ✅ Fix 3 — optional chaining
function greet(name: string | undefined) {
  const upper = name?.toUpperCase()       // string | undefined — safe
}

// ✅ Fix 4 — non-null assertion (only when you KNOW it's defined)
function greet(name: string | undefined) {
  const upper = name!.toUpperCase()       // tells TS "trust me, it's defined"
}
2

Use as const for Literal Types

string not assignable to 'red' | 'green' | 'blue'
2 min

TypeScript widens string literals to string by default. Use as const to preserve the literal type.

as const — prevent type widening
type Color = "red" | "green" | "blue"

// ❌ Error: string is not assignable to Color
const color = "red"           // TypeScript infers: string (widened)
const setColor = (c: Color) => {}
setColor(color)               // Error!

// ✅ Fix 1 — as const
const color = "red" as const  // TypeScript infers: "red" (literal)
setColor(color)               // ✅ works

// ✅ Fix 2 — explicit type annotation
const color: Color = "red"   // TypeScript checks against Color
setColor(color)               // ✅ works

// ✅ Fix 3 — as const on config objects
const ROUTES = {
  home: "/",
  about: "/about",
  contact: "/contact",
} as const

type Route = typeof ROUTES[keyof typeof ROUTES]
// Route = "/" | "/about" | "/contact"  (literal union, not string)
3

Fix Union Type Mismatches

Type does not satisfy the constraint
3 min

When assigning to a union type, the value must fully satisfy at least one member of the union — all required properties must be present.

Union type — satisfying one member
type SuccessResponse = { status: "success"; data: string[] }
type ErrorResponse   = { status: "error";   message: string }
type ApiResponse = SuccessResponse | ErrorResponse

// ❌ Error: object does not satisfy either member
const res: ApiResponse = { status: "success" }  // missing 'data'

// ✅ Fix — include all properties of the member you intend
const res: ApiResponse = { status: "success", data: ["item1"] }

// ✅ Discriminated union with type narrowing
function handle(res: ApiResponse) {
  if (res.status === "success") {
    console.log(res.data)      // TypeScript knows this is SuccessResponse
  } else {
    console.log(res.message)   // TypeScript knows this is ErrorResponse
  }
}
4

Fix Function Return Type Errors

Return type does not match declared type
3 min

When a function has an explicit return type, every code path must return a value matching that type. Missing branches or mismatched return values trigger this error.

Function return type — all paths covered
// ❌ Error: Function lacks ending return statement, type includes 'undefined'
function getLabel(status: "active" | "inactive" | "pending"): string {
  if (status === "active") return "Active"
  if (status === "inactive") return "Inactive"
  // "pending" case missing!
}

// ✅ Fix 1 — cover all cases
function getLabel(status: "active" | "inactive" | "pending"): string {
  if (status === "active") return "Active"
  if (status === "inactive") return "Inactive"
  return "Pending"   // covers "pending" and satisfies the return type
}

// ✅ Fix 2 — switch with exhaustive check
function getLabel(status: "active" | "inactive" | "pending"): string {
  switch (status) {
    case "active":   return "Active"
    case "inactive": return "Inactive"
    case "pending":  return "Pending"
    default:
      // Exhaustive check — TypeScript will error here if you add a new union member
      const _exhaustive: never = status
      return _exhaustive
  }
}
5

Fix Interface and Object Shape Mismatches

Property missing or incompatible
3 min

When TypeScript says a property is missing or incompatible, either add the missing property, make it optional, or adjust the interface to match your data.

Interface shape — fix missing properties
interface User {
  id: string
  name: string
  email: string
  role: "admin" | "user"
}

// ❌ Error: Property 'email' and 'role' are missing
const user: User = { id: "1", name: "Alice" }

// ✅ Fix 1 — add the missing properties
const user: User = { id: "1", name: "Alice", email: "a@b.com", role: "user" }

// ✅ Fix 2 — make properties optional in the interface (if appropriate)
interface User {
  id: string
  name: string
  email?: string    // optional
  role?: "admin" | "user"
}

// ✅ Fix 3 — use Partial<User> for partial objects (e.g. update payloads)
function updateUser(id: string, updates: Partial<User>) {
  // updates can have any subset of User properties
}
updateUser("1", { name: "Bob" })  // ✅ valid — email/role not required

Use Partial<T> for update payloads, Required<T> to make all properties mandatory, Pick<T, 'a' | 'b'> to select specific properties, and Omit<T, 'password'> to exclude sensitive fields.

Prevention

Frequently Asked Questions

What does 'Type X is not assignable to type Y' mean in TypeScript?+
It means you are trying to assign a value of one type to a variable, parameter, or return position that expects a different, incompatible type. TypeScript is telling you the shapes don't match — a property is missing, a value is too wide, or a type is completely different.
What is the difference between type widening and type narrowing?+
Type widening is when TypeScript infers a broader type than you intended — for example, inferring string instead of the literal 'hello'. Type narrowing is when you use conditions, typeof, instanceof, or type guards to tell TypeScript a value is a more specific type within a broader union.
When should I use 'as' type assertions?+
Use type assertions (as SomeType) only when you know more about the type than TypeScript can infer — for example, when working with DOM elements or API responses you have already validated. Avoid using as to silence errors you haven't actually fixed — it removes type safety.
What is 'as const' in TypeScript?+
as const makes TypeScript infer the most specific (narrowest) literal types for a value. Without it, TypeScript widens 'red' to string and ['a', 'b'] to string[]. With as const, 'red' stays as the literal type 'red' and the array becomes a readonly tuple. Useful for config objects, lookup tables, and discriminated unions.
How do I fix a TypeScript error when assigning to a union type?+
When assigning to a union type (A | B), the value must be fully assignable to at least one of the members. Check which union member you intend to satisfy and ensure your value has all the required properties of that member. You may need to add missing properties or use a type guard to narrow before assignment.
Should I use 'any' to fix type errors?+
No. Using any silences the error but removes all type checking for that value, defeating the purpose of TypeScript. Instead, use unknown for values you haven't checked yet, use type guards to narrow, or use proper generic types. Reserve any only for truly dynamic values where the type is unknowable.

Need Expert Help?

We Build Type-Safe TypeScript Applications

Softplix engineers write production TypeScript with strict mode, proper generics, and zero type suppressions. Need a TypeScript code review? Let us help.

Talk to an Engineer