Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When to 'implement' interface in angular

My question might be stupid, but i would like to get some clarity on this. As per my understanding, in angular we use interface to give idea about the data structure. So suppose we have an array of data of persons, we create an interface like

export interface Person {
  name: string;
  age: string
 }

And we can directly import this to our ts file and assign the type like

export class ComponentName implements OnInit {
data: Person[];
constructor() {}
ngOnInIt(){}
}

I totally understand this. But my confusion is, when do we ‘implement’ interface. I saw examples like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export class ComponentName implements OnInit, Person {
data: Person[];
constructor() {}
ngOnInIt(){}
}

Any help would be appreciatable. Thank you

>Solution :

In letter options, now your ComponentName MUST HAVE name and age fields 😉

so construct like this class X implements Y says literally that X is an Y

Also keep in mind that in typescript declarations does not matter, but actual structure, so even without implements Person delcaration, if ComponentName would have name and age fields, person:Person=this would still be valid.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading