Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to preserve original decorators (no transpiling) #20569

Closed
nashwaan opened this issue Dec 8, 2017 · 2 comments
Closed

Option to preserve original decorators (no transpiling) #20569

nashwaan opened this issue Dec 8, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@nashwaan
Copy link

nashwaan commented Dec 8, 2017

TypeScript Version: 2.6.2

Code

I like Typescript for all its advertised advantages and specially, it is a superset for ESNext javascript.

But I am facing one issue in the transpiled code.
I would like you to add an option to preserve original decorators in the emitted code.

Say I have the following class Greeter in greeter.ts:

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

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

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

But how to keep class and function decorators in the output and not introduce __decorate code?

Expected behavior:

I want to get this greeter.js output (which is correct ESNext syntax when experimental decorator is enabled, and it can be consumed by babel):

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;
  }
}

Actual behavior:

But currently I get this output using tsc greeter.ts --target ESNext (which is good for the general case):

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);
@j-oliveras
Copy link
Contributor

Duplicate of #16882 and #18713.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 11, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants