You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行ng build --prod报错:Property 'email' does not exist on type 'Object'

解决Angular生产构建时“Property 'email' does not exist on type 'Object'”错误

Hey, I’ve dealt with this exact error plenty of times when running ng build --prod! The root cause is TypeScript’s strict type checking—you declared loginObj and registerObj as Object types, but the native Object doesn’t have email, password, or name properties. Angular cranks up the type validation for production builds, which is why this pops up now even if it worked in development.

Here are your best solutions, ordered by how recommended they are:

方案一:定义专用接口(最规范,长期推荐)

Create TypeScript interfaces for your login and register objects to explicitly define their structure. This gives TypeScript clear type information and even helps with autocomplete in your IDE:

// Add these interfaces at the top of login.component.ts
interface LoginForm {
  email: string;
  password: string;
}

interface RegisterForm {
  email: string;
  name: string;
  password: string;
}

// Update your variable declarations
public loginObj: LoginForm = { email:'', password:'' };
public registerObj: RegisterForm = { email:'', name:'', password:'' };

This approach isn’t just a fix—it’s best practice for TypeScript development, as it catches typos and ensures your objects stay consistent.

方案二:类型断言(快速临时解决)

If you don’t want to write full interfaces right now, you can use type assertion to tell TypeScript what properties your objects have:

// Assert the type directly when declaring the objects
public loginObj = { email:'', password:'' } as { email: string; password: string };
public registerObj = { email:'', name:'', password:'' } as { email: string; name: string; password: string };

This works quickly, but it’s less maintainable than using interfaces—if you ever change the object structure, you’ll have to update the assertion everywhere.

方案三:改用any类型(不推荐,仅应急)

You can skip TypeScript’s type checking entirely by setting the type to any:

public loginObj: any = { email:'', password:'' };
public registerObj: any = { email:'', name:'', password:'' };

While this fixes the error immediately, it disables all type safety for these objects. Typos in property names won’t be caught during development, which can lead to hard-to-find bugs later. Only use this if you’re in a real pinch.

One last note: Development mode relaxes some type checks, which is why you didn’t see this error earlier. Sticking to strong typing with interfaces will save you from lots of production build surprises down the line!

内容的提问来源于stack exchange,提问作者sushanth dornala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:03:20