编译打字稿时收到错误消息

时间:2015-09-30 09:06:29

标签: typescript

我有以下代码:

class BasketManager {
    private persistPromises: { [key: string]: ng.IPromise<Basket.BasketModel> } = {};

    constructor(private $q: ng.IQService) {}

    order(basket: Basket.BasketModel, simulate = false): ng.IPromise<Basket.BasketModel> {
        return this.persistPromises[basket.basketId] = this.$q.when(this.persistPromises[basket.basketId] || basket).then(basket => {
            return this.$http.post(url, {})
                .then((response:ng.IHttpPromiseCallbackArg<API.Contracts.IBasketContract>) => this.storeBasket(response.data, true))
                .catch((response:ng.IHttpPromiseCallbackArg<any>) => this.handleBasketErrorResponse(basket, response));
        });
    });

    private storeBasket(data: API.Contracts.IBaseBasketContract, withItems = false): Basket.BasketModel {
        /* ... */
    }

    private handleBasketErrorResponse(basket: Basket.BasketModel, response: ng.IHttpPromiseCallbackArg<any>): Basket.BasketModel {
        /* ... */
    }
}

当我尝试编译它时,我收到此错误消息:

TypeScript error: app/scripts/objectmanager/basketmanager.service.ts(100,60): error TS2453: The type argument for type parameter 'TResult' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
  Type argument candidate 'IBaseBasketContract' is not a valid type argument because it is not a supertype of candidate 'BasketModel'.

100,60在上面的片段中是7,56。

我尝试显式指定每个变量的参数,但这没有帮助。我错过了什么?

1 个答案:

答案 0 :(得分:0)

好的,好像我自己找到了答案。用以下代码替换了带有错误的行:

return this.persistPromises[basket.basketId] = this.$q.when(this.persistPromises[basket.basketId] || basket).then<Basket.BasketModel>(basket => {