如何不转换类/函数装饰器,将它们保存在output()

时间:2017-12-08 07:29:10

标签: typescript tsc

说我在Greeter中有以下课程greeter.ts

function fooDecorator(constructor: Function) {
  Object.seal(constructor);
}

function barDecorator(value: boolean) {
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    descriptor.enumerable = value;
  };
}

@fooDecorator
class Foo {
  greeting: string;
  constructor(message: string) {
    this.greeting = message;
  }
  @barDecorator(false)
  greet() {
    return 'Hello, ' + this.greeting;
  }
}

我使用greeter.js获得了以下tsc greeter.ts --target ESNext输出:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function clsDecorator(constructor) {
  Object.seal(constructor);
}
function funDecorator(value) {
  return function (target, propertyKey, descriptor) {
    descriptor.enumerable = value;
  };
}
let Greeter = class Greeter {
  constructor(message) {
    this.greeting = message;
  }
  greet() {
    return 'Hello, ' + this.greeting;
  }
};
__decorate([
  funDecorator(false)
], Greeter.prototype, "greet", null);
Greeter = __decorate([
  clsDecorator
], Greeter);

如果我想直接使用它,这很好。

但是如何在输出中保留类和函数装饰器而不引入__decorate代码?

所需的greeter.js输出为:

function clsDecorator(constructor) {
  Object.seal(constructor);
}

function funDecorator(value) {
  return function(targe, propertyKey, descriptor) {
    descriptor.enumerable = value;
  };
}

@clsDecorator
class Greeter {
  greeting;
  constructor(message) {
    this.greeting = message;
  }
  @funDecorator(false)
  greet() {
    return 'Hello, ' + this.greeting;
  }
}

0 个答案:

没有答案