Common TypeScript Errors Explained: A Diagnostic Table for Inference, Generics, and Narrowing
debuggingerrorstype-inferencegenericsnarrowing

Common TypeScript Errors Explained: A Diagnostic Table for Inference, Generics, and Narrowing

TTypeScript Website Editorial Team
2026-11-27
6 min read

Diagnose common TypeScript errors with a scenario table, minimal examples, and a reusable checklist for inference, generics, and narrowing.

TypeScript errors often describe the symptom rather than the real cause. This diagnostic guide connects common messages—such as “type is not assignable,” “property does not exist,” generic constraint failures, and narrowing errors—to small, repeatable fixes you can apply in any project.

Overview

A TypeScript diagnostic is a clue about the relationship between your code and the types the compiler can prove. The fastest fix is usually not a cast. First identify which value, expression, or generic type is wider, narrower, optional, or otherwise different from what the operation requires.

When debugging, reduce the problem to three questions:

  1. What type did TypeScript infer? Hover over the value in your editor, or temporarily assign it to a variable with an explicit type.
  2. What type does the operation require? Inspect the function parameter, property, generic constraint, or union member involved.
  3. Which fact is missing? The fix may require validation, a type guard, a better generic constraint, or a corrected data model.

This approach is useful whether you are learning TypeScript, migrating JavaScript, or maintaining a React, Node.js, or API codebase. For a wider list of compiler codes, see the TypeScript Compiler Errors Reference.

Checklist by scenario

SymptomTypical causeFirst checkPreferred fix
“Type X is not assignable to type Y”A value is wider, narrower, or structurally different than the target type.Compare the exact property types, including null, undefined, and literal values.Correct the model, transform the value, or narrow it before assignment.
“Property does not exist on type”The property is not shared by every member of a union, or the object type is incomplete.Check whether the value is a union and whether a discriminant or guard is available.Narrow with in, a discriminant, or a custom type guard.
A value cannot be used as an object keyThe key is a general string, while the object accepts only known keys.Inspect keyof and the index expression.Type the key as keyof typeof object, or define an intentional index signature.
“Type does not satisfy the constraint”A generic argument does not meet the requirements declared with extends.Read the constraint as a contract: which properties or operations must exist?Pass a compatible type or revise the constraint to match the operation.
“Object is possibly undefined”Optional properties, array lookups, or nullable values have not been checked.Find the value’s path from its declaration to the failing line.Guard it, provide a default, or model the state so absence is handled explicitly.

1. Fixing “type is not assignable”

Consider this common mismatch:

type User = { id: string; name: string };

const user = { id: 42, name: "Mina" };
const selected: User = user;

The object has the right property names, but id is a number rather than a string. Decide which model represents the real boundary. If the API returns numeric IDs, change User or map the response. If the application requires strings, convert at the boundary:

const selected: User = { ...user, id: String(user.id) };

Do not solve a genuine mismatch with as User. An assertion changes what the compiler believes without changing the runtime value.

2. Fixing “property does not exist”

For a union, TypeScript permits only operations safe for every member:

type Result =
  | { status: "ok"; value: string }
  | { status: "error"; message: string };

function render(result: Result) {
  if (result.status === "ok") {
    return result.value;
  }
  return result.message;
}

The status field is a discriminant. Checking it narrows the union and makes each property available in the correct branch. If you do not control the type, an in check can provide a simpler guard:

if ("message" in result) {
  return result.message;
}

3. Fixing generic constraint errors

A generic constraint states the minimum shape a type must have:

function getId<T extends { id: string }>(item: T) {
  return item.id;
}

getId({ name: "Mina" }); // missing id

The call fails because the argument cannot satisfy the declared contract. Add the required property, use a different function for objects without IDs, or loosen the constraint only if the implementation no longer depends on id. A constraint should describe an operation the function genuinely performs, not serve as a general-purpose annotation.

4. Fixing union narrowing errors

Narrowing can be lost when a value is copied, mutated, or accessed through an imprecise alias. Keep the check close to the use, and prefer immutable discriminated unions:

function message(result: Result) {
  switch (result.status) {
    case "ok":
      return result.value;
    case "error":
      return result.message;
  }
}

If every union member should be handled, an exhaustive helper can expose future omissions:

function assertNever(value: never): never {
  throw new Error(`Unexpected value: ${String(value)}`);
}

Use it in a default branch when appropriate. This is especially useful when a state union grows over time.

What to double-check

  • Literal widening: A const object property may still be inferred as a general string unless you preserve its literal type. Use an explicit annotation or as const when the exact value matters.
  • Optional versus nullable: name?: string permits absence, while name: string | null requires the property but permits a null value. Handle the two states deliberately.
  • Array access: An index can be outside the array even when the index has type number. Check the result before using it, particularly in strict projects.
  • Runtime data: TypeScript types disappear at runtime. JSON, request bodies, environment variables, and database results still need validation before trusted code consumes them.
  • Generic inference: If inference chooses an unhelpful type, inspect the function’s parameter positions. An explicit type argument can help, but redesigning the signature may be clearer.
  • Compiler settings: Strictness changes which uncertain states are reported. Review the project’s tsconfig.json before comparing behavior across repositories. The strict mode rollout checklist covers a staged approach.

Common mistakes

Adding assertions until the build passes. Assertions are appropriate when you have verified an external invariant, but repeated assertions often hide an incorrect API type or missing runtime check.

Making everything optional. Adding question marks may silence an assignment error while spreading undefined handling throughout the application. Make a property optional only when absence is a valid state.

Using any at the boundary. A temporary escape hatch can be useful during migration, but it removes useful feedback downstream. Prefer unknown for untrusted values, then narrow or validate it.

Reading only the final line of a diagnostic. The useful detail is often the nested property path or the source location where a wider type was introduced. Follow the value backward rather than editing only the failing expression.

Confusing editor errors with build errors. Check the same TypeScript configuration and command used in CI. Different project files, generated declarations, or module settings can produce different diagnostics.

When to revisit

Return to this checklist whenever a TypeScript upgrade, compiler-flag change, dependency update, or API-model change produces unfamiliar diagnostics. Inference and error wording can evolve, but the underlying questions remain stable: what was inferred, what was required, and which runtime fact has not been proven?

Before a seasonal planning cycle or a larger refactor, review recurring errors rather than fixing them one by one. Group them by cause—incorrect boundary types, optional state, weak generics, or missing narrowing—and address the shared design issue. Add a small regression test for behavior that matters, and consider a type-level test for reusable public types. The TypeScript testing guide outlines where each kind of test fits.

For a practical next step, copy the failing code into the smallest reproducible example you can make, remove unrelated framework code, inspect the inferred types, and apply the narrowest sound fix. Then run the project’s type check and relevant tests. This habit turns common TypeScript errors from interruptions into useful documentation of your program’s contracts.

Related Topics

#debugging#errors#type-inference#generics#narrowing
T

TypeScript Website Editorial Team

Technical Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.