【发布时间】:2023-03-11 11:13:02
【问题描述】:
我使用的是 Angular CLI : 9.1.1,所以我尝试更新一些数据,而不是全部,
form: UserInfo = {adresse : {}};
UserInfo.interface
export interface UserInfo {
id_user: string;
username: string;
email: string;
nom: string;
prenom: string;
telephone: number;
password: string;
specialite: string;
adresse?: Address;
}
已应用此错误:
Type '{}' is missing the following properties from type 'Address': id_adresse, adresse, code_postal, ville, and 2 more.ts(2740)
user-info.ts(12, 5): The expected type comes from property 'adresse' which is declared here on type 'UserInfo'
这是有效的,但是当我重新启动我的应用程序时,项目没有启动。有人可以帮我解决这个问题。谢谢
【问题讨论】:
-
但属性缺失。更改您的类型定义以使其可选
-
我怎样才能让它们成为可选的
-
使用'?'像
id?: number这样的属性名称之后的运算符,如果您更喜欢这种方法,这将使它在类/接口上成为可选的。 -
在此处显示 UserInfo 定义。 (编辑您的问题)
-
正如@EliasSoares 所说,将“地址”中的类型定义更改为可选,
id_address?: number、adresse?: string;等。或者,您可以手动声明值。form: UserInfo = { adresse: { id_adresse: 1, adresse: "some address", ... } }.
标签: javascript angular typescript forms