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
⚡ The 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
// "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 guardWhat 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.
Narrow the Type with a Type Guard
string | undefined not assignable to stringWhen TypeScript infers string | undefined or string | null, you must narrow to string before using it where only string is accepted.
// ❌ 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"
}Use as const for Literal Types
string not assignable to 'red' | 'green' | 'blue'TypeScript widens string literals to string by default. Use as const to preserve the literal type.
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)Fix Union Type Mismatches
Type does not satisfy the constraintWhen assigning to a union type, the value must fully satisfy at least one member of the union — all required properties must be present.
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
}
}Fix Function Return Type Errors
Return type does not match declared typeWhen 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.
// ❌ 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
}
}Fix Interface and Object Shape Mismatches
Property missing or incompatibleWhen 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 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 requiredUse 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
- Read TypeScript errors from the bottom up — the last line usually names the exact property or value that's wrong
- Use as const on string and object literals you intend to use as literal types
- Never use any to silence type errors — use unknown, type guards, or proper generics instead
- Make optional properties explicit with ? instead of omitting them and hoping TypeScript accepts it
- Use Partial<T>, Required<T>, Pick<T>, and Omit<T> instead of defining duplicate interface variants
- Add exhaustive checks in switch statements so TypeScript catches missing union members at compile time
Frequently Asked Questions
What does 'Type X is not assignable to type Y' mean in TypeScript?+−
What is the difference between type widening and type narrowing?+−
When should I use 'as' type assertions?+−
What is 'as const' in TypeScript?+−
How do I fix a TypeScript error when assigning to a union type?+−
Should I use 'any' to fix type errors?+−
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