打字稿装饰混淆

时间:2016-01-30 23:43:06

标签: typescript decorator

我一直在玩TypeScript装饰器但是没有运气让他们工作。我阅读了How to implement a typescript decorator?以及http://blog.wolksoftware.com/decorators-reflection-javascript-typescript,我从中创建了装饰器实现

function log(target: any, key: string, descriptor?: any) {

  // save a reference to the original method
  // this way we keep the values currently in the 
  // descriptor and don't overwrite what another 
  // decorator might have done to the descriptor.
  if(descriptor === undefined) {
      descriptor = Object.getOwnPropertyDescriptor(target, key);
  }
  var originalMethod = descriptor.value; 

  //editing the descriptor/value parameter
  descriptor.value =  function (...args: any[]) {
      var a = args.map(a => JSON.stringify(a)).join();
      // note usage of originalMethod here
      var result = 12;//originalMethod.apply(this, args);
      var r = JSON.stringify(result);
      console.log(`Call: ${key}(${a}) => ${r}`);
      return result;
  }

  // return edited descriptor as opposed to overwriting 
  // the descriptor by returning a new descriptor
  return descriptor;
}

class C {
    constructor(){
        console.log("built");
    }
    @log
    public foo(n: number) {
        console.log("foo called");
        return n * 2;
    }
}
//new MethodDecoratorExample().method("hi");
var c = new C();
var r = c.foo(23); //  "Call: foo(23) => 12"
console.log(r);

然而,运行此代码并不会产生我所期望的,实际上它似乎没有覆盖描述符。挖掘更多我发现在生成的代码中,对__decorate的调用只有三个参数

__decorate([
    log
], C.prototype, "foo");

这意味着在__decorate中c = 3,因为没有传入描述符。因此,不返回新的描述符,代码流也会进行,就像方法没有被截获一样。

所以我必须错误地应用这个装饰器,但我看不出我做错了。它可能被解释为属性描述符吗?我确实在某个地方看到了一些评论,但这与将C语言中的foo方法定义为一个我不做的lambda有关。

我怎样才能使这个工作?

1 个答案:

答案 0 :(得分:1)

这里的问题是TypeScript默认会生成ES3代码,方法decorators generated for ES3显然与为ES5生成的方法有所不同。因此,除非您确实需要定位ES3,否则修复问题的简单方法是使用tsc --experimentalDecorators --target es5定位ES5,或使用tsc --experimentalDecorators --target es6定位ES6。

相关问题