【问题标题】:Typescript decorator deceiving 2 arguments, how to compile this?打字稿装饰器欺骗 2 个参数,如何编译这个?
【发布时间】:2017-01-25 04:19:47
【问题描述】:

我一直在阅读有关 typescript 和 ES7 中装饰器的更多信息。我尝试了这个简单的代码:

function decorator(...args) {
    console.log(args);
}

//@decorator
class foo {
    constructor() {}

    @decorator
    method() {}
}

let bar = new foo();
bar.method();

这是我在控制台中得到的:

$ npm install -g typescript@2.1.15
$ npm install -g @types/node
$ tsc --experimentalDecorators file.ts
$ node file.js
[ foo { method: [Function] }, 'method' ]

只有两个参数。

但是,如果我在打字稿操场上执行此操作,我会得到这个结果

Array[3]

其中 Arra[0] 和 Array[2] 是对象,而 Array[1] 是字符串。

这怎么可能?此外,我应该如何使用实验性装饰器正确编译打字稿?

我最好的问候...

【问题讨论】:

    标签: javascript typescript decorator typescript2.0


    【解决方案1】:

    解决了。根据https://www.typescriptlang.org/docs/handbook/compiler-options.html,默认目标是ES3。

    将目标设置为 ES5 即可解决问题:

    $ tsc --experimentalDecorators -t 'es5' t.ts && node t.js
    [ foo { method: [Function] },
      'method',
      { value: [Function],
        writable: true,
        enumerable: true,
        configurable: true } ]
    

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 2020-01-30
      • 2021-12-21
      • 2020-06-12
      • 1970-01-01
      • 2017-12-27
      • 2015-06-28
      相关资源
      最近更新 更多