2017-12-22 104 views
5

tôi bắt đầu sử dụng nguyên cảo 2.5 và bây giờ tôi nhận được tin nhắn này cho mã trong các file định nghĩa nguyên cảo góc:IHttpPromise sai kéo dài IPromise với nguyên cảo 2,5

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>; 
} 

Bây giờ nhận được một thông báo lỗi nói:

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 

Có ai có ý tưởng gì có thể sai không? Sẽ đánh giá cao bất kỳ lời khuyên nào bạn có thể đưa ra.

Để tham khảo đây là 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>; 
} 
+0

Phiên bản nào của góc bạn đang sử dụng? –

+0

Bạn đang nói Angular, nhưng typefiles của bạn là từ AngularJS. Angular bây giờ chỉ đề cập đến Angular 2+ trong khi Angular 1.x cũ được gọi là AngularJS. Vui lòng cập nhật câu hỏi và thẻ của bạn để tham khảo phiên bản chính xác. – jornare

Trả lời

6

Các bit của mã mà bạn cung cấp cho thấy rằng bạn đã có một phiên bản cũ của tập tin typings góc. Phiên bản này thực sự không chính xác (trong mắt của Typescript 2.4+) mở rộng IPromise<T>, và do đó không tương thích với v2.4 +.

contravariance nghiêm ngặt cho callback thông số

nguyên cảo "thắt chặt" các loại kiểm tra các thông số chức năng gọi lại trong 2.4.0, và thực hiện cải tiến hơn nữa trong 2.4.2. Đây là tài liệu trên "What's new in Typescript (2.4)" wiki và trên trang "Breaking changes" cho 2,4.

Ở cuối ngăn xếp lỗi trình biên dịch, có nghĩa là IHttpPromiseCallbackArg<T> không thể gán cho T. Vì vậy, các tập tin đánh máy bạn đã luôn luôn được "không chính xác" trong khía cạnh này, nhưng trình biên dịch không đủ thông minh để nhận ra nó như vậy cho đến v2.4.

Minh họa

Các Mappable<T> example là đáng kể tương tự như IPromise.then(). Chúng tôi có thể điều chỉnh ví dụ này bằng cách mở rộng giao diện:

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>; 
} 

Mã này sẽ biên dịch tốt với Bản 2.3.3. Nguyên cảo 2.4 sẽ (chính đáng) phàn nàn rằng R là không thể chuyển nhượng để T.


Như tôi đã đề cập, ví dụ này là (cơ bản) có cấu trúc giống với (một phiên bản rút gọn của) IPromise.then(). Chúng tôi có thể đổi tên các chức năng, giao diện, thông số và loại, cung cấp:

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>; 
} 

Một lần nữa, Typecript 2.3.3 trở lên sẽ chấp nhận mã này, trong khi Typecript 2.4+ sẽ không. Thay thế IHttpPromiseCallbackArg<T> cho R mang lại kết quả tương tự.

Cách khắc phục

Cài đặt một newer types file.

Một tập tin typings tương đối gần đây sẽ IHttpPromise<T> định nghĩa là

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

hoặc

type IHttpPromise<T> = IPromise<IHttpResponse<T>>; 
Các vấn đề liên quan