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

[Bug]: TypeScript annotation that contains the name of the annotated class method changes final output #16375

Closed
aweary opened this issue Mar 21, 2024 · 1 comment · Fixed by #16377
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@aweary
Copy link
Contributor

aweary commented Mar 21, 2024

Input code

REPL link

export class Database {
  transaction(
    // If the type annotation here contains the *name of the method*
    // then it changes how the method is compiled. Rename `transaction`
    // below and see how the output changes.
    callback: (transaction: Transaction) => unknown,
  ) {
    return this.table.transaction(
      (x) => callback(new DaoTransaction(x)),
    );
  }
}

Current and expected behavior

This is a class method called transaction that has a parameter called callback, which has the following type annotation

callback: (transaction: Transaction) => unknown

note that the parameter in the TypeScript annotation is called transaction, like the name of the method the parameter is defined for.

The TypeScript annotation should not affect how the code is compiled but what I've noticed is that the output is much different when the annotation contains the name of the method. When thats the case it outputs:

_proto.transaction = function (_transaction) {
    function transaction(_x) {
      return _transaction.apply(this, arguments);
    }
    transaction.toString = function () {
      return _transaction.toString();
    };
    return transaction;
  }(function (
  // If the type annotation here contains the *name of the method*
  // then it changes how the method is compiled. Rename `transaction`
  // below and see how the output changes.
  callback) {
    return this.table.transaction(function (x) {
      return callback(new DaoTransaction(x));
    });
  });

but if you were to remove the annotation or rename the parameter like

callback: (t: Transaction) => unknown

then the output is much simpler, skipping the wrapping function that writes toString for whatever reason

 _proto.transaction = function transaction(
  // If the type annotation here contains the *name of the method*
  // then it changes how the method is compiled. Rename `transaction`
  // below and see how the output changes.
  callback) {
    return this.table.transaction(function (x) {
      return callback(new DaoTransaction(x));
    });
  };

Environment

See: the REPL link

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

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

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 25, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants