在TypeScript升级后承诺返回类型错误 [英] Promise return type error after TypeScript upgrade

查看:223
本文介绍了在TypeScript升级后承诺返回类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 typescript v2.3.4,以下代码可以正常工作:

  getStuff():Promise< IStuff> {

返回this.http.get(URL,options)
.toPromise()
.then((res)=> ; {
let stuff:IStuff;
if(res.json()。data){
stuff = res.json()。data;
}
return东西;
})
.catch((reason)=> {
this.handleError(reason);
});
}

...其中 handleError 像这样:

  handleError =(error:any)=> {
this.debug(error);
抛出错误;
};

现在,带有打字稿 v2.4.1 I得到错误:'Type'Promise< void | IStuff>不能分配为 Promise< IStuff>类型。输入‘void |无法分配IStuff以键入 IStuff。 $ void类型不能分配给 IStuff类型。



我知道为什么会这样。



...但是这样做确实有效,对我来说,当其他代码不起作用时,它会起作用:

  getStuff():Promise< IStuff> {

返回this.http.get(URL,options)
.toPromise()
.then((res)=> {
让东西:IStuff;
if(res.json()。data){
stuff = res.json()。data;
}
退货;
})
.catch((原因)=> {
if(reason.status){
return Promise.reject (原因);
}否则{
this.handleError(reason);
}
});
}

...在 else 的情况下,它确实在执行上面生成错误的代码,但是没有错误。



我可以通过更改 handleError 为:

  handleError =(error:any):Promise<任何> => {
this.debug(error);
Promise.reject(错误);
};

...但是我很好奇具体的改变是什么导致了错误,为何添加 if / else 块的else路径与原始代码相同时,为什么能正常工作?

了解 2.4的注释。


Using typescript v2.3.4, the following code worked fine:

    getStuff() : Promise<IStuff>{

        return this.http.get(url, options)
            .toPromise()
            .then( (res) => {
                let stuff: IStuff;
                if (res.json().data){
                    stuff = res.json().data;
                }
                return stuff;
            })
            .catch( (reason) => {
                this.handleError(reason);
            });
    }

...where handleError is like this:

handleError = (error:any) => {
    this.debug(error);
    throw error;
}; 

Now, with typescript v2.4.1 I get the error: 'Type 'Promise<void | IStuff>' is not assignable to type 'Promise<IStuff>'. Type 'void | IStuff' is not assignable to type 'IStuff'. Type 'void' is not assignable to type 'IStuff'.'

I can see why that would be the case.

...but this DOES work, and it makes no sense to me that it would work when the other code does not:

getStuff() : Promise<IStuff>{

    return this.http.get(url, options)
        .toPromise()
        .then( (res) => {
            let stuff: IStuff;
            if (res.json().data){
                stuff = res.json().data;
            }
            return stuff;
        })
        .catch( (reason) => {
                if( reason.status) {
                    return Promise.reject(reason);
                } else {
                    this.handleError(reason);
                }
        });
}

...in the else case, it is doing exactly what the code above that generates the error is doing, and yet there's no error.

I can fix the problem by just changing handleError to be:

handleError = (error:any):Promise<any> => {
    this.debug(error);
    Promise.reject(error);
};

...but I'm curious what the specific change was that caused this to become an error, and why does adding the if/else block work correctly when it has an else path that is identical to the original code?

解决方案

With the TypeScript 2.4 release, TypeScript has become stricter with generics and Promise callbacks, which will cause issues with upgrades, especially those that use Promises. I've noticed this in several of my apps. You can read about it in the release notes for 2.4.

这篇关于在TypeScript升级后承诺返回类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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