Skip to content

TS2339: Property 'X' does not exist on type 'Y'

This error occurs when you try to access a property that doesn’t exist on the type according to TypeScript.


interface User {
name: string;
email: string;
}
const user: User = { name: "Alice", email: "alice@example.com" };
console.log(user.age);
// ❌ Property 'age' does not exist on type 'User'

Option 1: Add the property to the interface

Section titled “Option 1: Add the property to the interface”
interface User {
name: string;
email: string;
age?: number; // ✅ Made optional
}
const user: User = { name: "Alice", email: "alice@example.com" };
console.log(user.age); // ✅ Returns undefined

Option 2: Use a type assertion (if you know the runtime shape)

Section titled “Option 2: Use a type assertion (if you know the runtime shape)”
interface ExtendedUser extends User {
age: number;
}
const user = { name: "Alice", email: "alice@example.com", age: 30 };
console.log((user as ExtendedUser).age); // ✅

Option 3: Use index signatures for dynamic properties

Section titled “Option 3: Use index signatures for dynamic properties”
interface User {
name: string;
email: string;
[key: string]: any; // ✅ Allows any additional properties
}

const response = await fetch('/api/user');
const data = await response.json();
console.log(data.username);
// ❌ Property 'username' does not exist on type 'any'
// ✅ Fix: Define the expected shape
interface ApiResponse {
username: string;
email: string;
}
const data: ApiResponse = await response.json();
console.log(data.username); // ✅

  • Define all properties in your interfaces
  • Use optional properties (?) for fields that might not exist
  • Prefer type definitions over any