🔧 Error Fixes

Fix: TypeScript — Property does not exist on type


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

TypeScript doesn’t know that property exists on the object’s type.

Fix 1: Define the type

// ❌ Empty object type has no properties
const user = {};
console.log(user.name);  // Error!

// ✅ Define the shape
interface User {
  name: string;
  email: string;
}
const user: User = { name: "Alice", email: "alice@example.com" };
console.log(user.name);

Fix 2: Type API responses

// ❌ fetch returns unknown shape
const data = await response.json();
console.log(data.users);  // Error!

// ✅ Type the response
interface ApiResponse {
  users: User[];
  total: number;
}
const data: ApiResponse = await response.json();
console.log(data.users);

Fix 3: Use type narrowing

// ❌ Could be multiple types
function handle(input: string | number) {
  console.log(input.toUpperCase());  // Error! number has no toUpperCase
}

// ✅ Narrow the type
function handle(input: string | number) {
  if (typeof input === "string") {
    console.log(input.toUpperCase());  // Safe
  }
}

Fix 4: Index signature for dynamic keys

// ❌ Dynamic keys not allowed
const config = {};
config["theme"] = "dark";  // Error!

// ✅ Add index signature
const config: Record<string, string> = {};
config["theme"] = "dark";

// ✅ Or use a specific interface
interface Config {
  [key: string]: string;
}

Fix 5: Extend Window or global types

// ❌ Custom property on window
window.myApp.init();  // Error!

// ✅ Declare it
declare global {
  interface Window {
    myApp: { init: () => void };
  }
}

See also: TypeScript type not assignable fix | TypeScript cheat sheet