Home > Enterprise >  Angular Argument of type 'array' is not assignable to parameter of type 'string'
Angular Argument of type 'array' is not assignable to parameter of type 'string'

Time:05-31

I have json:

  public example=[{
       array:[item1, item2,]
    },
    {
       array:[item1, item2,]
    }]

file html:

<div *ngFor="let item of example">
      <div *ngIf="check(item.array)"> ...</div>
</div>

but call function check inside ngOnInit(), it error

 check(...request: string[]){
        return this._RolesService.checkRoles(request);
    }

ngOnInit(): void {
       let req:string[] = ['string1'];
        if(this.check(req)){}
}

gender html is ok, so When i call function check() in ngOnInit, it had error: Argument of type 'string[]' is not assignable to parameter of type 'string'.

CodePudding user response:

You should change your code into:

let req:string[] = ['string1'];
if(this.check(...req)){}

Or

let req = 'string1';
if(this.check(req)){}

CodePudding user response:

Can you please share the code of this._RolesService.checkRoles(request)? I think this function accepts a string as parameter, but you are passing ['string1'] which is an array of string. That is why the error is thrown.

Based on the current info you have provided, I think this is the issue.

Corrected solution: Thanks to the comments under this solution, one of the ways to solve your issue is to use the spread operator when calling the method:

this.check(...req)
  • Related