Home > Software engineering >  When to 'implement' interface in angular
When to 'implement' interface in angular

Time:02-11

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

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

Any help would be appreciatable. Thank you

CodePudding user response:

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.

  • Related