James McClatchey
James McClatchey

Reputation: 13

Typing a Promise in Typescript 2.0

The following compiled and worked fine in Typescript 1.8 and Angular 1. Now our development platform is moving to Typescript 2.3. The same function fails to compile in Typescript 2.3 at the return statement:

"Type 'IPromise<{}>' is not assignable to type 'IPromise'"

How do you type a Promise? Is there a cast?

detail(id: string): ng.IPromise<IInvOrder> {
    var defer = this.$q.defer();
    this.$http.get(this.apiUrl + "/Detail/" + id)
        .success((result: IInvOrder) => defer.resolve(result))
        .error((err: any) => defer.reject(err));
    return defer.promise;
}

Upvotes: 0

Views: 505

Answers (1)

basarat
basarat

Reputation: 276293

Fix

TypeScript now forces you to use explicit generics when they cannot be inferred.

Example

Change var defer = this.$q.defer(); to var defer = this.$q.defer<IInvOrder();

Upvotes: 3

Related Questions