gpt4 book ai didi

angular - 错误 TS2322 : Type 'Observable<{} | T>' is not assignable to type 'Observable'

转载 作者:行者123 更新时间:2023-12-05 09:14:59 24 4
gpt4 key购买 nike

我发现这个用于 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'.

全类

enter image description here

关于如何修复有什么建议吗?

最佳答案

这是因为您执行了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com