Skip to content

[Bug]: typescritpt constructor order #15268

Description

@volovikoff

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

class Base{
}
class A extends Base {
    b = this.a.length;
    constructor(public a: string) {
        super();
     }
}

Configuration file name

No response

Configuration

No response

Current and expected behavior

"use strict";

function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
class Base {}
class A extends Base {
  constructor(a) {
    super();
    _defineProperty(this, "b", this.a.length);
    this.a = a;
  }
}
new A('dasdasd');

this will throw an error "Cannot read properties of undefined (reading 'length')"

Environment

angular 15 (es2022) and playground

Possible solution

In compiled js in constructor _defineProperty has to go after constructor parameters assignment.

Additional context

No response

Activity

  1. babel-bot commented on Dec 13, 2022

    @babel-bot
    Collaborator

    Hey @volovikoff! 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.

  2. self-assigned this
    on Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions