【问题标题】:error TS2322: Type 'Observable<{} | T>' is not assignable to type 'Observable<T>'错误 TS2322:类型 'Observable<{} | T>' 不可分配给类型 'Observable<T>'
【发布时间】:2018-11-18 10:06:57
【问题描述】:

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

全班

关于如何修复的任何建议?

【问题讨论】:

  • 如何调用这个构造函数?传递给它的变量类型是什么?
  • 现在在问题中添加了全班的照片。

标签: angular rxjs angular7


【解决方案1】:

这是因为你做了return of();,而传递给of的值不是T类型。

您可以改为使用 return of(null);,或确保将 T 类型的值传递给 of 函数。

【讨论】:

    【解决方案2】:

    试试这个声明:

    readonly data$: Observable<{} | T>;
    

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 2017-12-29
      • 2019-01-31
      • 1970-01-01
      • 2018-03-09
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      相关资源
      最近更新 更多