[Fixed] TypeScript – Unable to retrieve value from array


I have an interface Country as below in my component.ts class :

export interface Country{
  id: String;
  name: String;
  checked: false; 

const country: Country[] = [

   { id: 'India', name: 'India', checked: false},
   { id: 'USA', name: 'USA', checked: false},
   { id: 'Canada', name: 'Canada', checked: false},


selectedCountry: Country[] ;

On UI I have checkboxes for respective countries.

Let’s say if user selects India/USA/Canada ( one at a time). So to handle the same, I am trying to write an if-else statement as :

 // do something;
else if(this.selectedCountry.includes('USA')
 // do something;
else ...  

But I am getting an error : Argument of type ‘string’ is not assignable to parameter of type ‘Country’.

Can someone help me out figure the issue. Any help/pointers are highly appreciable.


Try this

this.selectedCountry.some(c => c.name === "India");

The some() method checks if any of the elements in an array pass a
test (provided as a function).

For reusable code :

checkCountry(name: string): boolean{
  return this.selectedCountry.some(c => c.name === name);

Leave a Reply

(*) Required, Your email will not be published