Angular 8:错误 TS2322:类型 'Observable<CustomType>' 不可分配给类型 'Observable<CustomType[]>'

时间:2021-03-31 23:33:51

标签: angular angular-material observable

我有一个角度组件,但我收到错误“Observable”不可分配给类型“Observable”。我对 Angular 还很陌生,无法调试。能否请你帮忙。以下是组件:

export interface SearchGroup {
  type: string;
  list: any[];
}


export class CustomComponent implements OnInit {

  
  options: Observable<SearchGroup[]>;
  returnData = [];
  suggestions: SearchGroup[] = [];
  

  constructor() {}

  ngOnInit() {
    

    this.suggestions = [{
      type: 'Type 1',
      list: [{a:1},{b:2}]
      },{
      type: 'Type 2',
      list: [{c:3},{d:4}]
      },
    ];

    this.options =  this.form.get('formControl')!.valueChanges
        .pipe(debounceTime(300),
            startWith(''),
            switchMap(userInput => this.filterList(userInput))
        );

  }

  filterList(value) : SearchGroup[] {
    let filterValue = (value || "").toLowerCase();
    let returnVal =  this.suggestions
                         .map(group => ({type: group.type,list:this._filter(group.list,filterValue)}))
                         .filter(group=> group.list.length > 0);
    return returnVal;
  }

  _filter(list,filterValue){
    return list.filter(item=> item['id'].toLowerCase().includes(filterValue));
  }

这样,我收到以下错误:

 error TS2322: Type 'Observable<SearchGroup>' is not assignable to type 'Observable<SearchGroup[]>'.
      Type 'SearchGroup' is missing the following properties from type 'SearchGroup[]': length, pop, push, concat, and 26 more.

0 个答案:

没有答案