不能将类型"void"分配给类型"ObservableInput< {}>"

不能将类型

问题描述:

在我迁移到TS 2.2.2之后,此错误开始弹出,因此我认为这是问题所在……代码没有停止工作,但是现在我收到了该错误,我尝试了一些类似返回的操作一个空的可观察对象,捕获重新抛出的异常并返回一个对象,似乎没有任何作用.为什么现在会发生这种情况?它不应该理解我要重新抛出异常并且不期望返回吗?我误读了错误吗?

This error started to pop up after I migrated to TS 2.2.2, so I'm assuming that's the problem... The code did not stop working, but now I receive that error and I tried a few things like returning an empty observable, catching the re-thrown exception and returning an object, nothing seemed to work. Why is this happening now? Shouldn't it understand I'm re-throwing the exception and not expect a return? Am I misreading the error?

这是完整的错误描述:

这是完整的代码:

return request
    .map((res: Response) => res.json())
    .catch((error: any) => {
        // todo: log?

        if (error.status == 500) {
            this.alertService.showError(error.statusText);
        } else if (error.status == 588) {
            this.alertService.showAlert(error.statusText);
        }

        Observable.throw(error.statusText);
    });

我尝试返回Observable,但是我的包装器方法期望返回类型为T的返回值,这是我的反序列化请求(map(...))的返回值.如果我确实返回了throw,这是我得到的错误:

I tried returning the Observable, but my wrapper method expects a return of type T, which is the return of my deserialized request (map(...)). If I do return the throw this is the error I get:

[ts]类型可观察"不能分配给类型"T"

[ts] Type 'Observable' is not assignable to type 'T'

我正在使用:

  • Angular4
  • 打字稿2.2.2
  • Angular4
  • Typescript 2.2.2

您必须返回Observable

you have to return the Observable

 return request
    .map((res: Response) => res.json())
    .catch((error: any) => {
        // todo: log?

        if (error.status == 500) {
            this.alertService.showError(error.statusText);
        } else if (error.status == 588) {
            this.alertService.showAlert(error.statusText);
        }

        return Observable.throw(error.statusText);
    });