我发现这个用于 Angular 的异步管道错误处理的示例看起来很有希望:https://sebastian-holstein.de/post/error-handling-angular-async-pipe/
尝试以 angular 7 运行它会导致编译错误
readonly data$: Observable<T>;
constructor(data: Observable<T>) {
this.data$ = data.pipe(
shareReplay(1),
catchError(error => {
console.log(error);
this._errorLoading$.next(true);
return of();
})
);
}
错误:
ERROR in src/app/loading-wrapper.ts(12,5): error TS2322: Type 'Observable<{} | T>' is not assignable to type 'Observable<T>'.
Type '{} | T' is not assignable to type 'T'.
Type '{}' is not assignable to type 'T'.
全类
关于如何修复有什么建议吗?
最佳答案
这是因为您执行了return of();
,而传递给of
的值不是T
类型。
您可以改为执行 return of(null);
,或者确保将类型 T 的值传递给 of
函数。
关于angular - 错误 TS2322 : Type 'Observable<{} | T>' is not assignable to type 'Observable<T>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53359721/