Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IHttpPromise使用TypeScript 2.5错误地扩展了IPromise_Javascript_Angular_Typescript - Fatal编程技术网

Javascript IHttpPromise使用TypeScript 2.5错误地扩展了IPromise

Javascript IHttpPromise使用TypeScript 2.5错误地扩展了IPromise,javascript,angular,typescript,Javascript,Angular,Typescript,我开始使用Typescript 2.5,现在在Angular Typescript定义文件中的代码中得到以下消息: interface IHttpPromise<T> extends IPromise<T> { success(callback: IHttpPromiseCallback<T>): IHttpPromise<T>; error(callback: IHttpPromiseCallback<any>): I

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

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>;
}
接口IHttpPromise扩展了IPromise{
成功(回调:IHttpPromiseCallback):IHttpPromise;
错误(回调:IHttpPromiseCallback):IHttpPromise;
然后(successCallback:(响应:IHttpPromiseCallbackArg)=>IPromise,errorCallback?:(响应:IHttpPromiseCallbackArg)=>any):IPromise;
然后(successCallback:(响应:IHttpPromiseCallbackArg)=>TResult,errorCallback?:(响应:IHttpPromiseCallbackArg)=>any):IPromise;
}
现在收到一条错误消息,上面说:

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
严重性代码描述项目文件行抑制状态错误TS2430(TS)接口“IHttpPromise”错误地扩展了接口“IPromise”。“then”属性的类型不兼容。
类型“{(successCallback:(response:IHttpPromiseCallbackArg)=>IPromise,errorCa…”不能分配给类型“{(successCallback:(promiseValue:T)=>IHttpPromise,errorCallback?:(原因:…”。
参数“successCallback”和“successCallback”的类型不兼容。
参数“promiseValue”和“response”的类型不兼容。
类型“IHttpPromiseCallbackArg”不可分配给类型“T”。管理员C:\H\admin\admin\lib\typings\angularjs\angular.d.ts 1273处于活动状态
有没有人知道什么地方出了问题?如果你能给我一些建议,我将不胜感激

以下是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>;
}
接口IPromise{
/**
*无论承诺何时或将何时得到解决或拒绝,只要结果可用,就会异步调用其中一个成功或错误回调。回调使用一个参数进行调用:结果或拒绝原因。此外,notify回调可能被调用零次或多次,以提供进度指示n、 在承诺被解决或拒绝之前。
*
*此方法返回一个新的承诺,该承诺通过successCallback的返回值errorCallback进行解析或拒绝。它还通过notifyCallback方法的返回值进行通知。无法通过notifyCallback方法解析或拒绝该承诺。
*/
然后(successCallback:(promiseValue:T)=>ihttpromise,errorCallback?:(原因:any)=>any,notifyCallback?:(状态:any)=>any):IPromise;
/**
*无论承诺何时或将何时得到解决或拒绝,只要结果可用,就会异步调用其中一个成功或错误回调。回调使用一个参数进行调用:结果或拒绝原因。此外,notify回调可能被调用零次或多次,以提供进度指示n、 在承诺被解决或拒绝之前。
*
*此方法返回一个新的承诺,该承诺通过successCallback的返回值errorCallback进行解析或拒绝。它还通过notifyCallback方法的返回值进行通知。无法通过notifyCallback方法解析或拒绝该承诺。
*/
然后(successCallback:(promiseValue:T)=>IPromise,errorCallback?:(原因:any)=>any,notifyCallback?:(状态:any)=>any):IPromise;
/**
*无论承诺何时或将何时得到解决或拒绝,只要结果可用,就会异步调用其中一个成功或错误回调。回调使用一个参数进行调用:结果或拒绝原因。此外,notify回调可能被调用零次或多次,以提供进度指示n、 在承诺被解决或拒绝之前。
*
*此方法返回一个新的承诺,该承诺通过successCallback的返回值errorCallback进行解析或拒绝。它还通过notifyCallback方法的返回值进行通知。无法通过notifyCallback方法解析或拒绝该承诺。
*/
然后(successCallback:(promiseValue:T)=>TResult,errorCallback?:(原因:任意)=>TResult,notifyCallback?:(状态:任意)=>any):i推荐;
/**
*promise.then的缩写(null,errorCallback)
*/
捕获(onRejected:(原因:any)=>IHttpPromise:i批准;
/**
*promise.then的缩写(null,errorCallback)
*/
catch(onRejected:(原因:any)=>IPromise:IPromise;
/**
*promise.then的缩写(null,errorCallback)
*/
catch(onRejected:(原因:any)=>TResult):i推荐;
/**
*允许您观察承诺的履行或拒绝,但不修改最终值。这有助于释放资源或进行清理,无论承诺是否被拒绝或解决。有关详细信息,请参阅完整规范。
*
*因为finally在JavaScript中是一个保留字,ES3不支持保留关键字作为属性名,所以您需要调用promise['finally'](回调)等方法,以使代码IE8和Android 2.x兼容。
*/
最后(最终回调:()=>any):IPromise;
}

您提供的代码位表明您拥有一个旧版本的角度打字文件。这个版本确实不正确(在Typescript 2.4+的眼中)扩展了
IPromise
,因此与v2.4+不兼容

回调参数的严格逆变

Typescript在2.4.0中“加强”了回调函数参数的类型检查,并在2.4.2中做了进一步改进。这在、和2.4的页面上有记录

在编译器错误堆栈的底部,
ihttpromisecallbackarg
不可分配给
t
。因此,在这方面,键入文件一直是“不正确的”,但直到v2.4,编译器才足够聪明地识别它

插图

这是一个显著的错误
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>;
}
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>;
}
interface IHttpPromise<T> extends IPromise<IHttpPromiseCallbackArg<T>> {   }
type IHttpPromise<T> = IPromise<IHttpResponse<T>>;