簡體   English   中英

角度+矩形+打字稿裝飾錯誤

[英]angular + restangular + typescript decorators error

我正在使用:

  • 角度1.4
  • 矩形模型
  • 打字稿作為語言

這是我的代碼:

  function plain(){
    return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
      var originalMethod = descriptor.value; // save a reference to the original method
      descriptor.value = function(...args: any[]) {
        var result = originalMethod.apply(this, args);
        return result.then(function stripRestangularAttributes(response){
          return response.plain();
        });
      };
      return descriptor;
    };
  }

  export class XYZ {
    @plain
    public getSomethingBySomething(data: Data): restangular.IPromise<any> {
      if (!entityKey && !period) {
        return null;
      }
      return this.restangularElement.all("route1/route2").post(data);
    }
  }

我收到以下錯誤:

錯誤TS1241:作為表達式調用時,無法解析方法裝飾器的簽名。 提供的參數與呼叫目標的任何簽名都不匹配。

它被拋出@plain行。

一些信息:

  • 矩形方法,在本例中為.post(data) ,返回promise

我想要:

  • 使用打字稿方法裝飾器在promise鏈中鏈接單個元素。 chain元素是一個thenable的對象 ,它將在矩形的結果上調用.plain() ,因此我將在任何方法上使用@plain ,所有結果將自動得到plain() ed
  • 換句話說,當restangular返回一個promise時(就像往常一樣),我想將其鏈接到每個附加了@plain裝飾器的方法: @plain .then(function stripRestangularAttributes(response){ return response.plain(); })

不幸的是,在上面的示例中,我無法理解打字稿所抱怨的是什么。

PS:我一直在閱讀TS裝飾器指南中給出的出色答案

根據規范,您的方法裝飾器不應返回函數:

declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;

嘗試以下方法:

function plain(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
    var originalMethod = descriptor.value; // save a reference to the original method
    descriptor.value = function(...args: any[]) {
        var result = originalMethod.apply(this, args);
        return result.then(function stripRestangularAttributes(response){
            return response.plain();
        });
    };
    return descriptor;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM