【问题标题】:error TS2339: Property 'email' does not exist on type 'Object'错误 TS2339:“对象”类型上不存在属性“电子邮件”
【发布时间】:2018-07-10 09:20:09
【问题描述】:

我正在使用 Angular 5,但出现错误,代码如下:

signIn(provider) {
    this._auth.login(provider).subscribe(
      (data) => {
        console.log(data);
        this.hideForm = false;

        this.emaill = data.email;
        this.nom = data.last_name;
        this.prenom = data.first_name;
        this.profileImage = data.image;
    })
}

错误是:

src/app/authentification/authentification.component.ts(34,28):错误 TS2339:“对象”类型上不存在属性“电子邮件”。 src/app/authentification/authentification.component.ts(35,25):错误 TS2339:“对象”类型上不存在属性“last_name”。 src/app/authentification/authentification.component.ts(36,28):错误 TS2339:“对象”类型上不存在属性“first_name”。 src/app/authentification/authentification.component.ts(37,34):错误 TS2339:“对象”类型上不存在属性“图像”。

【问题讨论】:

    标签: angular


    【解决方案1】:

    使用[] 提供了一种在访问JavaScript 对象 时将变量的实际值用作key/property 的绝佳方式。

    例如- someObject[someKey]

    (data: object) => {
        this.emaill = data['email'];
    }
    

    如果仍然存在错误或想要未来证明编码,请添加检查 -

    if (data.hasOwnProperty('email')) {
        this.email = data['email'];
    }
    

    【讨论】:

      【解决方案2】:

      最受好评的答案不是很好,因为它违背了@Alex Lomia 也提到的 Typescript 的全部目的

      解决方案是 -

      1. 要解决此问题,请创建一个接口来描述您的架构并扩展 mongoose.Document: 在这里检查这个类似的问题 - similar question

      2. 使用 Javascript 而不是 TypeScript ,如果有人在后端使用 NestJS 最有可能遇到这个错误,所以如果你不想经历制作接口的过程和 DTO 的使用 javascript ,也可以使用内置的 javascript文件也会更小。

      【讨论】:

        【解决方案3】:

        你可以使用data:any:

        signIn(provider) {
            this._auth.login(provider).subscribe(
              (data:any) => {
                console.log(data);
                this.hideForm = false;
        
                this.emaill = data.email;
                this.nom = data.last_name;
                this.prenom = data.first_name;
                this.profileImage = data.image;
            })
        }
        

        现在它可以正常工作了,因为我遇到了同样的问题。

        【讨论】:

          【解决方案4】:

          我的Nodejs Typescript 项目中也出现了同样的错误-

          代码-

          app.post('/form-submit', (req, res) => {
             const errors = {};
             if (...condition) {
                errors.email = ['Email is not valid.'];
             }
          });
          

          错误 -

          Property 'email' does not exist on type '{}'.ts(2339)
          

          解决方案(添加:any 并解决错误)-

          const errors:any = {};
          

          【讨论】:

            【解决方案5】:

            “对象”类型上不存在属性“主机名”。

            我正在使用 Vue 和 typescript,在加载一些数据后尝试 console.log this.dropdownItems[0].hostname 时在调试时发生错误。对我来说,错误看起来是对声明的 typescript 类型检查变量:

            dropdownItems: Array<Object> = []
            

            应该是:

            dropdownItems: any[] = []
            

            源自: https://www.reddit.com/r/ionic/comments/9cxc79/property_courses_does_not_exist_on_type_object/

            【讨论】:

              【解决方案6】:

              我个人遇到过这个问题。原来的代码是:

              if (response.data !== 'undefined') {
                  // ...
              }
              

              应该是:

              if (response["data"] !== 'undefined') {
                  // ...
              }
              

              【讨论】:

                【解决方案7】:

                将第 3 行的 (data) 替换为 (data : any)

                【讨论】:

                • 谢谢,但它给了我另一个错误:ts(40,3): error TS1005: ',' expected.
                • 我的意思是在最后一个}
                • 功能没有问题。检查整个文件中是否存在其他可能的语法错误。
                • 给新人的注意事项:添加:any时,请确保将整个内容括在括号中。例如,当您想将data 转换为any 时,请使用(data: any)。另一个例子,如果你想将result 转换为any 是:(result: any)。确保使用(),否则您将面临 TSLint 错误。希望这对某人有所帮助。
                • 不是一个好的解决方案,指定 any 只是关闭类型检查,这违背了 Typescript 的全部目的。
                【解决方案8】:

                检查对象的条件是否为空

                if(data!==null && data!==undefined && data.email !==undefined)
                {
                    this.emaill = data.email;
                    this.nom = data.last_name;
                    this.prenom = data.first_name;
                    this.profileImage = data.image;
                }
                

                【讨论】:

                  【解决方案9】:

                  发生错误是因为 data 的类型是 Object ,它没有您的任何属性。您可以检查对象是否具有您的属性并使用它:

                  if (data.hasOwnProperty('email')) this.email = data['email'];
                  

                  【讨论】:

                    【解决方案10】:

                    或更好的实践来使用接口。 你可以声明接口:

                    export interface DataI {
                    
                     email: string;
                     ...
                     constructor(...){this.email = email .....}
                    

                    然后将其用作类型

                    【讨论】:

                      猜你喜欢
                      • 2018-07-16
                      • 2020-07-07
                      • 1970-01-01
                      • 2020-12-13
                      • 2019-01-21
                      • 2016-08-13
                      • 2018-08-29
                      • 2019-09-13
                      • 2020-10-18
                      相关资源
                      最近更新 更多