Home > Mobile >  The type 'string' is not assignable to type
The type 'string' is not assignable to type

Time:10-24

I'm getting this issue after upgrading angular version to the latest in my project:

The type 'string' is not assignable to type 'ReportTeamFilter[]'

the problem is when I do searchOptions.BusinessLst = this.business.value;

business = new FormControl('');
var searchOptions: DelegateSearch = new DelegateSearch();
searchOptions.BusinessLst = this.business.value;

delegate.ts

import { ReportingFilter }
export class DelegateSearch {
public BusinessLst: ReportingTeamFilter[];
constructor() {}
}

reporting-model.ts

export class ReportingTeamFilter {

public Id: number;
public Name: string;
public Date: Date;

constructor(){}
}

Any idea on how to fix this issue?

Thank you

CodePudding user response:

I am pretty sure that angular's new typed form feature is responsible for the error produced. Nevertheless the feature probably did not work before during runtime, so I assume it's a good thing that the error is now thrown during development phase.

The error is the following:

  • Your are defining business = new FormControl('');, which means that this.business.value will return a string (before typed forms it would return any).
  • Here you are declaring BusinessLst as a different type: public BusinessLst: ReportingTeamFilter[];
  • Now you are trying to assign the string to this array type here searchOptions.BusinessLst = this.business.value;

That's why the error is thrown. The types simply do not match. You should specify what the correct types are and your error should be gone. Unfortunately I am not able to help you with that, since I do not know the business logic behind these types.

CodePudding user response:

You are expecting array of ReportingTeamFilter and instead of that you are receiving string.

Change BusinessLst to be type string

// From:
public BusinessLst: ReportingTeamFilter[];

// To:
public BusinessLst: string[];
  • Related