使用RxJS编写程序以按术语搜索github存储库。
错误:类型Observable<string | Repositories>
无法分配给类型Observable<string | ResponseModel>
。如何解决此错误?
interface ResponseModel {
count: number;
items: Repositories[];
};
interface Repositories {
title: string;
};
const searchField: HTMLInputElement = document.querySelector('.search-field') as HTMLInputElement;
const repositories: HTMLUListElement = document.querySelector('.repositories') as HTMLUListElement;
const sequence$ = fromEvent(searchField, 'input');
const clearResult = ():void => {
if (repositories) {
repositories.innerHTML = '';
}
};
const sequence2$: Observable<string | ResponseModel> = sequence$
.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(clearResult),
switchMap((event: KeyboardEvent) => request(event)
.pipe(catchError(error => of('Error: ${'Not found'}')))
)
);
sequence2$.subscribe(result => showRepo(result));
function request(event: KeyboardEvent): Observable<Repositories> {
const url: string = `${'https://api.github.com/search/repositories'}?q=${(event.target as HTMLInputElement).value}`;
return from(fetch(url).then(res => res.json()));
};
function showRepo(res: ResponseModel) {...}
}
<input class="search-field" type="text">
<ul class="repositories"></ul>