Skip to content

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

@aweary

Description

@aweary

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions