【问题标题】:Type '{}' is missing the following properties - Angular 9.1.1类型“{}”缺少以下属性 - Angular 9.1.1
【发布时间】: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?: numberadresse?: string; 等。或者,您可以手动声明值。 form: UserInfo = { adresse: { id_adresse: 1, adresse: "some address", ... } }.

标签: javascript angular typescript forms


【解决方案1】:

使用? 运算符使属性可选,这表示编译器在实例化时不期望值。

export interface UserInfo {
    id_user?: string;
    username?: string;
    email?: string;
    nom?: string;
    prenom?: string;
    telephone?: number;
    password?: string;
    specialists?: string;
    adresse?: Address;
   
}

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 2021-03-24
    • 2019-06-21
    • 2020-09-28
    • 2021-04-20
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多