我有一个角度组件,但我收到错误“Observable”不可分配给类型“Observable
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.