首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Typescript装饰器欺骗2个参数,怎么编译这个?

Typescript装饰器欺骗2个参数,怎么编译这个?
EN

Stack Overflow用户
提问于 2017-01-25 12:19:47
回答 1查看 58关注 0票数 0

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

代码语言:javascript
复制
function decorator(...args) {
    console.log(args);
}

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

    @decorator
    method() {}
}

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

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

代码语言:javascript
复制
$ npm install -g typescript@2.1.15
$ npm install -g @types/node
$ tsc --experimentalDecorators file.ts
$ node file.js
[ foo { method: [Function] }, 'method' ]

只有两个论点。

但是,如果我在typescript操场中执行此命令,则会得到以下结果

代码语言:javascript
复制
Array[3]

其中Arra和Array2是对象,Array1是字符串。

这怎么可能呢?此外,我应该如何用实验性的装饰器正确地编译typescript?

我向你致以最诚意的问候。

EN

回答 1

Stack Overflow用户

发布于 2017-01-25 12:27:53

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

将目标设置为ES5可以解决这个问题:

代码语言:javascript
复制
$ tsc --experimentalDecorators -t 'es5' t.ts && node t.js
[ foo { method: [Function] },
  'method',
  { value: [Function],
    writable: true,
    enumerable: true,
    configurable: true } ]

致以问候。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41843317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档