简体   繁体   English

“字符串”类型的参数不能分配给“字符串[]”类型的参数

[英]Argument of type 'string' is not assignable to parameter of type 'string[]'

I've been scouring the web for a solution to this, what probably is, a minor problem.我一直在网上寻找解决方案,这可能是一个小问题。

My function on one ts.我在一个 ts 上的功能。 file:文件:

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;

}

The function on the mssql-service.ts file 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);
}

and the HANDLE ERROR for prosperity...和处理繁荣的错误......

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);

} }

the ERROR I GET on compile is this:我在编译时遇到的错误是:

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

When I change the :any here from:当我更改 :any 这里的内容时:

getTheHelpAnswer(jsonObj: any): any { 

to

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

this is the error I get:这是我得到的错误:

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[]'.

I don't understand what the problem is.我不明白问题是什么。

UPDATE1:更新1:

I forgot to add the signature of the FUNCTION我忘了添加 FUNCTION 的签名

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

But I still get this...但我仍然得到这个...

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[]'.

UPDATE 2:更新 2:

I figured it out...我想到了...

See solution where I answered my own question....请参阅我回答自己问题的解决方案....

Here's the solution:这是解决方案:

I changed this function:我改变了这个功能:

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;

}

to this:对此:

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;

}

and on the mssql-connect.service.ts file, the receiving function并在 mssql-connect.service.ts 文件中,接收函数

Changing this:改变这一点:

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);
}

to this...到这...

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 ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “string[]”类型的参数不可分配给“string”类型的参数 - Argument of type 'string[]' is not assignable to parameter of type 'string' 日期类型的参数不能分配给字符串类型的参数 - Argument of type date is not assignable to parameter of type string 类型“ X”的参数不能分配给类型“字符串”的参数 - Argument of type 'X' is not assignable to parameter of type 'string' “文件”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'File' is not assignable to parameter of type 'string' “字符串”类型的参数不可分配给“IScriptEditorProps”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'IScriptEditorProps' “字符串”类型的参数不可分配给“Blob”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'Blob' '[string]' 类型的参数不可分配给 'U' 类型的参数 - Argument of type '[string]' is not assignable to parameter of type 'U' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' “T”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'T' is not assignable to parameter of type 'string' '{}[]' 类型的参数不可分配给'string' 类型的参数 - Argument of type '{}[]' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM