Skip to content

[Bug]: Property redefining leads to it being set to undefined #14291

Closed

Description

@nowifi4u

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

interface BaseOptions {
  test1?: boolean;
}

abstract class BaseClass {
  public readonly options: BaseOptions;

  public constructor (options: BaseOptions) {
    this.options = options;

    console.log('BaseClass this', this);
  }
}

interface ChildOptions extends BaseOptions {
  test2?: number;
}

class ChildClass extends BaseClass {
  public readonly options!: ChildOptions;

  public constructor (options: ChildOptions) {
    super(options);

    console.log('ChildClass this', this);
  }
}

export const t = new ChildClass({
  test1: true,
  test2: 2,
});

Configuration file name

babel.config.json

Configuration

{
  "parserOpts": {
    "strictMode": true
  },
  "sourceMaps": "inline",
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": { "node": "current" },
        "modules": "commonjs"
      }
    ],
    "@babel/preset-typescript"
  ],
  "plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }], "@babel/plugin-proposal-class-properties"]
}

Current and expected behavior

Current behavior:
_defineProperty is present in the generated code in the constructor after super call

class BooleanResolvableType extends _Base.BaseType {
  constructor(options) {
    super(options);

    _defineProperty(this, "options", void 0);

    console.log('BooleanResolvableType constructor options', options);
    console.log('BooleanResolvableType constructor this', this);
  }

  async run(val, context) {
    console.log('BooleanResolvableType run this.options', this.options);
    const lval = val.toLowerCase();
    return await this.options.resolver(lval, context);
  }
}

Expected behavior:
_defineProperty should not be present in the constructor for the fields defined in a parent class

Environment

System:
OS: Windows 10 10.0.19044
Binaries:
Node: 16.13.0
npm: 8.3.1
npmPackages:
@babel/plugin-proposal-class-properties: 7.16.7
@babel/plugin-proposal-decorators: 7.17.2
@babel/preset-env: 7.16.11
@babel/preset-typescript: 7.16.7
eslint: 8.6.0
jest: 27.4.7

Possible solution

No response

Additional context

No response

Activity

  1. babel-bot commented on Feb 21, 2022

    @babel-bot
    Collaborator

    Hey @nowifi4u! 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. nicolo-ribaudo commented on Feb 21, 2022

    @nicolo-ribaudo
    Member

    This is the expected behavior to match what JavaScript does. You can test it by running this code in your browser's console:

    class A {
      x = 1
    }
    class B extends A {
      x
    }
    
    console.log("x is: " + new B().x); // undefined

    The current TypeScript behavior of ignoring x in B is wrong, but you can enable the useDefineForClassFields in your TS config to make it work correctly.

    If you want to ignore x in the subclass, you can use the declare modifier:

    class B extends A {
      declare x;
    }

    TS playground

  3. added
    outdated
    on May 26, 2022
  4. locked as resolved and limited conversation to collaborators on May 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescripti: questionoutdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions