不能将类型"void"分配给类型"ObservableInput< {}>" [英] Type 'void' is not assignable to type 'ObservableInput<{}>'

查看:776
本文介绍了不能将类型"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);
    });

这篇关于不能将类型"void"分配给类型"ObservableInput< {}>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆