Back to Blog
Bug Fixes•1 min read

Fix TypeScript 'Property does not exist on type' Error

Scaleup Infotech Team
Scaleup Infotech Team

Custom Software & Business Automation

TypeScriptTypesNarrowing

In this guide

A simple guide to this issue: TypeScript 'Property does not exist on type' Error. Learn why it happens, what to check first, and how to fix it.

Error TS2339 is TypeScript doing its job: you are reading a property it cannot prove exists. The right fix is almost never any — it is giving the compiler the type information it is missing.

The Error

Property 'name' does not exist on type '{}'. (ts2339)

Fix 1: Type Your API Responses

response.json() returns any/unknown. Tell TS what shape it is:

ts
interface User { id: string; name: string; email: string; }

const res = await fetch("/api/user");
const user = (await res.json()) as User;
console.log(user.name); // ✅ known property

Fix 2: Narrow Union Types Before Access

ts
type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "square"; side: number };

function area(s: Shape) {
  if (s.kind === "circle") return Math.PI * s.radius ** 2; // narrowed
  return s.side ** 2;
}

Fix 3: Index Signatures for Dynamic Keys

ts
const config: Record<string, string> = {};
config["theme"] = "dark"; // ✅ any string key allowed

Fix 4: Augment a Third-Party Type

When a library is missing a property you need (e.g. a custom field on the Express Request), declare it once via module augmentation rather than casting everywhere.

Avoid the any Escape Hatch

Casting to any silences the error but throws away every guarantee TypeScript gives you. Prefer typing the source. If you must escape, use unknown and narrow.

Relevant Scaleup Infotech Services

Share this article:

Keep Reading

Ready to implement these ideas?

Discuss Your Project