Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i solve this Error Subsequent property declarations must have the same type and Duplicate identifier. Angular 9

* I really dont know and dont understand why this is happening, im new using Angular *

  • Below i will let my full code and the complete error

ErrorSubsequent property declarations must have the same type. Property 'post1' must be of type 'Posts', but here has type 'any' and Duplicate identifier 'post1'

Post class

export class Posts{
    Key:string;
    Email:string;
    Password:string;
}

AppComponent class where i use the post class

export class AppComponent {
  title = 'proyecto-nro2';

  post1 = new Posts();
//Said the error above in every post1.
  post1.Key = 'NONE';
  post1.Email = 'NONE';
  post1.Password = '2';
}
like image 795
Hguedez Avatar asked Nov 25 '25 05:11

Hguedez


1 Answers

Properties can't initialised directly on a class in ES6. Initialise the post object inside the constructor and set the properties.

export class AppComponent {
  title = 'proyecto-nro2';

  post1: Posts;

 constructor(){
  this.post1 = new Posts();
  this.post1.Key = 'NONE';
  this.post1.Email = 'NONE';
  this.post1.Password = '2';

 }
}

Other Option:

class Posts {
    Key: string;
    Email: string;
    Password: string;

    constructor(key: string, email: string, password: string) {
        this.Key = key;
        this.Email = email;
        this.Password = password;
    }
}

export class AppComponent {
  title = 'proyecto-nro2';
  post1: Posts = new Posts('NONE', 'NONE', '2');
}
like image 154
nayakam Avatar answered Nov 28 '25 03:11

nayakam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!