typescript 类型为“string”的参数不能赋值给类型为“string[]”的参数

lb3vh1jj  于 2022-12-30  发布在  TypeScript
关注(0)|答案(1)|浏览(436)

我一直在网上搜索这个问题的解决方案,可能是一个小问题。
我在一个ts.文件上的函数:

public getHelpContent(question: string[]) {

  let theHelp: any[] = [];
  theHelp = this.mssqlService.getTheHelpAnswer(question);
  console.log("THE HELP:", theHelp);
  this.commentContent = theHelp ;

  let foundContent: any[] = [];
  for (let i = 0; i < this.commentContent.length; i++) {
    let hitContent: string[];
    hitContent = this.searchHelpItem(question, this.commentContent[i]);
    if (hitContent) {
      foundContent.push(hitContent);
    }
  }

  return theHelp;

}

mssql-service. ts文件上的函数

getTheHelpAnswer(jsonObj: any): any {
  let body = JSON.stringify(jsonObj);
  console.log("BODY VAR: ", body);
  return this.http
    .post<any>(`${this.urlLocation}notification/TheHelp`, body)
    .map((response: Response) => response.json())
    .catch(this.handleError);
}

和繁荣的处理错误...

private handleError(error: Response | any) {

  let errMsg: string;
  if (error instanceof Response) {
    const body = error || '';
    const err = JSON.stringify(body);
    errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Observable.throw(errMsg);

}
编译时出现错误如下:

error TS2345: Argument of type 'string' is not assignable to parameter of type 'string[]'

当我将此处的:any从:

getTheHelpAnswer(jsonObj: any): any {

getTheHelpAnswer(jsonObj: any): Observable<any> { ...

这是我得到的错误:

ERROR in src/app/app-help-state-machine.ts(364,5): error TS2322: Type 'Observable<any>' is not assignable to type 'any[]'.
  Property 'includes' is missing in type 'Observable<any>'.
src/app/app-help-state-machine.ts(371,50): error TS2345: 
Argument of type 'string' is not assignable to parameter of type 'string[]'.

我不明白问题出在哪里。
更新1:
我忘记添加FUNCTION的签名

public getHelpContent(question: string[]): any[] { ...

但我还是明白...

ERROR in src/app/app-help-state-machine.ts(364,5): error TS2322: Type 
'Observable<any>' is not assignable to type 'any[]'.
      Property 'includes' is missing in type 'Observable<any>'.
src/app/app-help-state-machine.ts(371,50): error TS2345: Argument of type 'string' is not assignable to parameter of type 'string[]'.

更新2:
我想通了...
查看我回答自己问题的解决方案....

xxe27gdn

xxe27gdn1#

解决方案如下:
我更改了此函数:

public getHelpContent(question: string[]) {

  let theHelp: any[] = [];
  theHelp = this.mssqlService.getTheHelpAnswer(question);
  console.log("THE HELP:", theHelp);
  this.commentContent = theHelp ;

  let foundContent: any[] = [];
  for (let i = 0; i < this.commentContent.length; i++) {
    let hitContent: string[];
    hitContent = this.searchHelpItem(question, this.commentContent[i]);
    if (hitContent) {
      foundContent.push(hitContent);
    }
  }

  return theHelp;

}

致:

public getHelpContent(question: string[]): string[] {

  const questionInfo = {
    "question": question
  }

  let theHelp = this.mssqlService.getTheHelpAnswer(questionInfo, question);
  console.log("THE HELP:", theHelp);

//Commented this out... as it caused the issue

//    this.commentContent = theHelp;
//
//    let foundContent: any[] = [];
//    for (let i = 0; i < this.commentContent.length; i++) {
//      let hitContent: string[];
//      hitContent = this.searchHelpItem(question, this.commentContent[i]);
//      if (hitContent) {
//        foundContent.push(hitContent);
//      }
//    }

  return lucyHelp;

}

在mssql-connect.service.ts文件中,接收函数
更改此内容:

getTheHelpAnswer(jsonObj: any): any {
   let body = JSON.stringify(jsonObj);
   console.log("BODY VAR: ", body);
   return this.http
       .post<any>(`${this.urlLocation}notification/TheHelp`, body)
       .map((response: Response) => response.json())
       .catch(this.handleError);
}

到这个...

getTheHelpAnswer(jsonObj: any, question: string[]): string[] {
   let body = JSON.stringify(jsonObj);
   console.log("BODY VAR: ", body);

   let result = this.http
     .post<any>(`${this.urlLocation}notification/TheHelp${question}`, body)
     .map((response: Response) => response.json())
     .catch(this.handleError);

   console.log("RESULT FROM HTTP: ", result);

  return result ;
}

相关问题