Loading...
Loading...
Property 'name' does not exist on type '{}'You're trying to access a property that TypeScript doesn't know exists on the type. Either the property is genuinely missing, the type definition is wrong, or you need to define an interface.
Create a TypeScript interface that describes the shape of your object.
// ❌ TypeScript doesn't know what's in the object
const user = {};
console.log(user.name); // 💥 Property 'name' does not exist
// ✅ Define the interface
interface User {
name: string;
email: string;
age: number;
}
const user: User = { name: "Alice", email: "a@b.com", age: 30 };
console.log(user.name); // ✅Assert the type when you know the shape of an API response.
interface ApiUser {
id: number;
name: string;
}
const response = await fetch('/api/user');
const user = await response.json() as ApiUser;
console.log(user.name); // ✅If the property might not exist, mark it as optional in the interface.
interface User {
name: string;
nickname?: string; // optional
}
const user: User = { name: "Alice" };
console.log(user.nickname?.toUpperCase()); // ✅ safe