2017-12-22 151 views
5

zacząłem używać maszynopis 2.5 i teraz otrzymuję komunikat o kodzie w kątowym pliku definicji maszynopis:IHttpPromise nieprawidłowo rozciąga IPromise z maszynopis 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>; 
} 

Teraz otrzymuję komunikat o błędzie:

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 

Czy ktoś ma jakieś pomysły, co może być nie tak? Byłbym wdzięczny za wszelkie rady, które możesz udzielić.

Dla porównania oto 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

Jaka wersja kątowa używasz? –

+0

Mówisz Angular, ale twoje kroje są z AngularJS. Angular teraz odnosi się tylko do Angular 2+, podczas gdy stary Angular 1.x jest określany jako AngularJS. Zaktualizuj swoje pytanie i tagi, aby znaleźć odpowiednią wersję. – jornare

Odpowiedz

6

Bity kodu Państwo przewidziane sugerują, że masz starą wersję pliku typowania kątowe. Ta wersja rzeczywiście jest niepoprawna (w porównaniu do Typeescript 2.4+) z rozszerzeniem IPromise<T>, a zatem nie jest kompatybilna z v2.4 +.

Strict kontrawariancja dla zwrotnego Parametry

maszynopis „zaostrzone” typ sprawdzania parametrów funkcji zwrotnej w 2.4.0, wykonane dalsze usprawnienia w 2.4.2. Jest to udokumentowane na stronie "What's new in Typescript (2.4)" wiki oraz na stronie "Breaking changes" dla wersji 2.4.

U dołu stosu błędów kompilatora ma sens, że IHttpPromiseCallbackArg<T> nie można przypisać do T. Tak więc plik typowania, który posiadasz, zawsze był "niepoprawny" pod tym względem, ale kompilator nie był wystarczająco inteligentny, aby rozpoznać go jako taki, aż do v2.4.

Ilustracja

Mappable<T> example jest zadziwiająco podobna do IPromise.then(). Możemy zaadaptować ten przykład poprzez rozszerzenie interfejsu:

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

Ten kod będzie się kompilował dobrze z opisem 2.3.3. Nagranie 2.4 nie (prawnie) skarżą się, że R nie jest przypisane do T.


Jak już wspomniano, w tym przykładzie jest (zasadniczo) konstrukcyjnie identyczne z (a odchudzić wersji) IPromise.then(). Możemy zmienić nazwę funkcji, interfejsów, parametry i rodzaje, podając:

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

Ponownie, maszynopis 2.3.3 lub wcześniej zaakceptuje ten kod, a Typescript 2.4+ nie będzie. Zastąpienie IHttpPromiseCallbackArg<T> dla daje taki sam wynik.

The Fix

Instalowanie newer types file.

Plik stosunkowo niedawne typowania byłby IHttpPromise<T> zdefiniowany jako

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

lub

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