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

TypeScript: public/private class constructor argument does not transpile default parameter #8692

Closed
MayhemYDG opened this Issue Sep 12, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@MayhemYDG

MayhemYDG commented Sep 12, 2018

Bug Report

Current Behavior
The public/private keywords in a class constructor's argument prevents the default parameter from being transpiled.

Input Code

export default class Example {
  constructor(public arg1 = null) { }
}

Expected behavior/code
I've enabled the env preset, configured to target ie11, so the default parameter should be transpiled to something ie11 can work with.

The transpiled code looks like this:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Example = function Example(arg1 = null) {
  _classCallCheck(this, Example);
};

exports.default = Example;

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": [
    ["@babel/env", {
      "useBuiltIns": "entry"
    }],
    "@babel/typescript",
    "@babel/react"
  ],
  "plugins": [
    ["@babel/transform-runtime", { "useESModules": true }],
    ["@babel/proposal-decorators", { "legacy": true }],
    ["@babel/proposal-class-properties", { "loose": true }]
  ]
}

Environment

  • Babel version(s): v7.0.0
  • Node/npm version: Node 10.10.0/npm 6.4.1
  • OS: Windows 10
  • Monorepo: yes
  • How you are using Babel: loader

Possible Solution
Removing the public/private keywords does generate good code:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Example = function Example() {
  var arg1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

  _classCallCheck(this, Example);
};

exports.default = Example;

You can also check how TS transpiles this piece of code: https://www.typescriptlang.org/play/#src=export%20default%20class%20Example%20%7B%0D%0A%20%20constructor(public%20arg1%20%3D%20null)%20%7B%20%7D%0D%0A%7D

define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Example = /** @class */ (function () {
        function Example(arg1) {
            if (arg1 === void 0) { arg1 = null; }
            this.arg1 = arg1;
        }
        return Example;
    }());
    exports.default = Example;
});

Additional context/Screenshots
this.arg1 = arg1; is also missing from the constructor, as you can see from the TS transpilation result above. This is already addressed there: #7074 #8669 #8682

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Sep 12, 2018

Collaborator

Hey @MayhemYDG! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Sep 12, 2018

Hey @MayhemYDG! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment