IHttpPromise错误地使用TypeScript 2.5扩展了IPromise [英] IHttpPromise incorrectly extends IPromise with TypeScript 2.5

查看:76
本文介绍了IHttpPromise错误地使用TypeScript 2.5扩展了IPromise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Typescript 2.5,现在我在Angular Typescript定义文件中得到以下消息:

I started to use Typescript 2.5 and now I get this message for code in the Angular typescript definition file:

interface IHttpPromise<T> extends IPromise<T> {
    success(callback: IHttpPromiseCallback<T>): IHttpPromise<T>;
    error(callback: IHttpPromiseCallback<any>): IHttpPromise<T>;
    then<TResult>(successCallback: (response: IHttpPromiseCallbackArg<T>) => IPromise<TResult>, errorCallback?: (response: IHttpPromiseCallbackArg<any>) => any): IPromise<TResult>;
    then<TResult>(successCallback: (response: IHttpPromiseCallbackArg<T>) => TResult, errorCallback?: (response: IHttpPromiseCallbackArg<any>) => any): IPromise<TResult>;
}

现在收到一条错误消息:

Now getting an error message saying:

Severity    Code    Description Project File    Line    Suppression State Error TS2430  (TS) Interface 'IHttpPromise<T>' incorrectly extends interface 'IPromise<T>'.   Types of property 'then' are incompatible.
    Type '{ <TResult>(successCallback: (response: IHttpPromiseCallbackArg<T>) => IPromise<TResult>, errorCa...' is not assignable to type '{ <TResult>(successCallback: (promiseValue: T) => IHttpPromise<TResult>, errorCallback?: (reason:...'.
      Types of parameters 'successCallback' and 'successCallback' are incompatible.
        Types of parameters 'promiseValue' and 'response' are incompatible.
          Type 'IHttpPromiseCallbackArg<T>' is not assignable to type 'T'.  admin   C:\H\admin\admin\lib\typings\angularjs\angular.d.ts 1273    Active

有人有什么主意吗?将不胜感激您可以提供的任何建议.

Does anyone have any ideas what could be wrong? Would appreciate any advice you can give.

作为参考,这里是IPromise:

For reference here's the IPromise:

interface IPromise<T> {
    /**
     * Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
     *
     * This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback. It also notifies via the return value of the notifyCallback method. The promise can not be resolved or rejected from the notifyCallback method.
     */
    then<TResult>(successCallback: (promiseValue: T) => IHttpPromise<TResult>, errorCallback?: (reason: any) => any, notifyCallback?: (state: any) => any): IPromise<TResult>;
    /**
     * Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
     *
     * This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback. It also notifies via the return value of the notifyCallback method. The promise can not be resolved or rejected from the notifyCallback method.
     */
    then<TResult>(successCallback: (promiseValue: T) => IPromise<TResult>, errorCallback?: (reason: any) => any, notifyCallback?: (state: any) => any): IPromise<TResult>;
    /**
     * Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.
     *
     * This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback. It also notifies via the return value of the notifyCallback method. The promise can not be resolved or rejected from the notifyCallback method.
     */
    then<TResult>(successCallback: (promiseValue: T) => TResult, errorCallback?: (reason: any) => TResult, notifyCallback?: (state: any) => any): IPromise<TResult>;

    /**
     * Shorthand for promise.then(null, errorCallback)
     */
    catch<TResult>(onRejected: (reason: any) => IHttpPromise<TResult>): IPromise<TResult>;
    /**
     * Shorthand for promise.then(null, errorCallback)
     */
    catch<TResult>(onRejected: (reason: any) => IPromise<TResult>): IPromise<TResult>;
    /**
     * Shorthand for promise.then(null, errorCallback)
     */
    catch<TResult>(onRejected: (reason: any) => TResult): IPromise<TResult>;

    /**
     * Allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.
     *
     * Because finally is a reserved word in JavaScript and reserved keywords are not supported as property names by ES3, you'll need to invoke the method like promise['finally'](callback) to make your code IE8 and Android 2.x compatible.
     */
    finally<TResult>(finallyCallback: () => any): IPromise<TResult>;
}

推荐答案

您提供的一些代码表明您有Angular Types文件的旧版本.该版本确实确实错误地扩展了IPromise<T>(在Typescript 2.4+看来),因此与v2.4 +不兼容.

The bits of code you provided suggest that you've got an old version of the Angular typings file. This version does indeed incorrectly (in the eyes of Typescript 2.4+) extend IPromise<T>, and thus is not compatible with v2.4+.

回调参数的严格相反性

Typescript在2.4.0中加强了"对回调函数参数的类型检查,并在2.4.2中进行了进一步改进. "Typescript(2.4)中的新增功能" Wiki ,并在页,用于2.4.

Typescript "tightened up" the type checking for callback function parameters in 2.4.0, and made further improvements in 2.4.2. This is documented on the "What's new in Typescript (2.4)" wiki, and on the "Breaking changes" page for 2.4.

在编译器错误堆栈的底部,确实有道理IHttpPromiseCallbackArg<T>不可分配给T.因此,在这方面,您输入的文件始终不正确",但是直到v2.4之前,编译器都不足够聪明来识别它.

At the bottom of the compiler error stack, it does make sense that IHttpPromiseCallbackArg<T> isn't assignable to T. So the typings file you have has always been "incorrect" in this respect, but the compiler wasn't smart enough to recognize it as such until v2.4.

插图

Mappable<T>示例IPromise.then()非常相似.我们可以通过扩展接口来修改此示例:

The Mappable<T> example is remarkably similar to IPromise.then(). We can adapt this example by extending the interface:

interface Mappable<T> {
    map<U>(f: (x: T) => U): Mappable<U>;
}

type R = {};

interface SubMappable<T> extends Mappable<T> {
    map<U>(f: (x: R) => U): Mappable<U>;
}

此代码可以使用Typescript 2.3.3很好地编译. Typescript 2.4将(理所应当)抱怨R不可分配给T.

This code will compile fine with Typescript 2.3.3. Typescript 2.4 will (rightfully) complain that R is not assignable to T.

正如我提到的,此示例在结构上(基本上)与IPromise.then()(精简版)相同.我们可以重命名函数,接口,参数和类型,如下所示:

As I mentioned, this example is (essentially) structurally identical to (a slimmed down version of) IPromise.then(). We can rename the functions, interfaces, parameters, and types, giving:

interface MyPromise<T> {
    then<TResult>(successCallback: (promiseValue: T) => TResult): MyPromise<TResult>;
}

type R = {};

interface MyHttpPromise<T> extends MyPromise<T> {
    then<TResult>(successCallback: (response: R) => TResult): MyPromise<TResult>;
}

同样,Typescript 2.3.3或更早版本将接受此代码,而Typescript 2.4+将不接受.将IHttpPromiseCallbackArg<T>替换为R会得到相同的结果.

Again, Typescript 2.3.3 or earlier will accept this code, while Typescript 2.4+ will not. Substituting IHttpPromiseCallbackArg<T> for R yields the same result.

修复

安装新类型文件.

相对较新的打字文件将IHttpPromise<T>定义为

A relatively recent typings file would have IHttpPromise<T> defined as

interface IHttpPromise<T> extends IPromise<IHttpPromiseCallbackArg<T>> {   }

type IHttpPromise<T> = IPromise<IHttpResponse<T>>;

这篇关于IHttpPromise错误地使用TypeScript 2.5扩展了IPromise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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