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

Typescript constructor arguments get thrown away (with example repo) #14515

Closed
tobyhinloopen opened this issue Apr 8, 2021 · 2 comments
Closed

Comments

@tobyhinloopen
Copy link

tobyhinloopen commented Apr 8, 2021

Describe the bug

Storybookjs's standard out-of-the-box compiler configuration for react typescript applications incorrectly compile Typescript code by removing some constructor bodies. Specifically, my class:

my time.ts class

export class Time {
  constructor(private time = 0) {
  }

  now() {
    this.time = (this.time || 0) + 1;
    return this.time;
  }
}

is compiled to:

BROKEN JS code as generated by storybookjs

var Time = /*#__PURE__*/function () {
  function Time() {
    var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

    _classCallCheck(this, Time);
  }

  _createClass(Time, [{
    key: "now",
    value: function now() {
      this.time = (this.time || 0) + 1;
      return this.time;
    }
  }]);

  return Time;
}();

Note how this.time = time is missing. The correct JS code should be something like:

CORRECT JS code as generated by default typescript compiler

export class Time {
    constructor(time = 0) {
        this.time = time;
    }
    now() {
        this.time = (this.time || 0) + 1;
        return this.time;
    }
}

To Reproduce

Expected behavior

  • The shown source should somehow set this.time to time
  • It should say This: 9001 should be 9001.

System

Environment Info:

  System:
    OS: Windows 10 10.0.19041
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
  Binaries:
    Node: 15.11.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 7.6.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 89.0.4389.114
    Edge: Spartan (44.19041.423.0), Chromium (89.0.774.68)
  npmPackages:
    @storybook/addon-actions: ^6.2.5 => 6.2.5
    @storybook/addon-essentials: ^6.2.5 => 6.2.5
    @storybook/addon-links: ^6.2.5 => 6.2.5
    @storybook/react: ^6.2.5 => 6.2.5
@tobyhinloopen tobyhinloopen changed the title Typescript constructor body incorrectly compiled (with example repo) Typescript constructor arguments get thrown away (with example repo) Apr 8, 2021
@tannerlyons
Copy link

Seems like a dupe of #12019

@tobyhinloopen
Copy link
Author

tobyhinloopen commented Apr 19, 2021

It is

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

No branches or pull requests

3 participants