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

Incorrect ES5 transpilation for 'this' usage in lambda as a constructor default parameter #55637

Closed
nevedaren opened this issue Sep 5, 2023 · 3 comments Β· Fixed by #56130
Closed
Assignees
Labels
Bug A bug in TypeScript Domain: JS Emit The issue relates to the emission of JavaScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros

Comments

@nevedaren
Copy link

nevedaren commented Sep 5, 2023

πŸ”Ž Search Terms

ES5, this alias

πŸ•— Version & Regression Information

  • This changed between versions 5.1.6 and 5.2.2

⏯ Playground Link

https://www.typescriptlang.org/play?target=1&ts=5.2.2#code/MYGwhgzhAEDqBOYAOSCm9oG8BQ1rAHsA7CAF3gFdhSD4AKXPaANzBAEsATMU1aAXmh0KJMADNUALiwBfAJQCAfNFIALdhAB0rDt17DREuQpxMWbLj1R1M8gNyMZ2RknjtWvc7qt1SATzQAVUMpaDAiPzlpACMCAhBUcKxGPHhUUgp4IhVKVAc8JydsIlQAdzhEFHQ6OQcgA

πŸ’» Code

// @target: es5
// @showEmit: true
class Wrapper {
  constructor(
    validate = (unsafe: {}) => this.validate(unsafe)) {
    validate({});
  }

  private validate(typeUnsafe: any): boolean {
    return true;
  }
}

new Wrapper();

Target: ES5

πŸ™ Actual behavior

Cannot read properties of undefined (reading 'validate') 

πŸ™‚ Expected behavior

No error (as with untranspiled version or with TS5.1.6)

Additional information about the issue

No response

@andrewbranch andrewbranch added Bug A bug in TypeScript Domain: JS Emit The issue relates to the emission of JavaScript labels Sep 7, 2023
@andrewbranch andrewbranch modified the milestone: Backlog Sep 7, 2023
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Sep 7, 2023
@typescript-bot
Copy link
Collaborator

The change between v5.1.6 and v5.2.2 occurred at f9cf821.

@andrewbranch
Copy link
Member

andrewbranch commented Sep 7, 2023

@rbuckton this appears to be possibly a regression from #54505 (the repro bot just detected an emit change, so it could be a red herring)

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 8, 2023

πŸ‘‹ Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @nevedaren

πŸ‘ Compiled
Emit:

"use strict";
var Wrapper = /** @class */ (function () {
    function Wrapper(validate) {
        if (validate === void 0) { validate = function (unsafe) { return this.validate(unsafe); }; }
        validate({});
    }
    Wrapper.prototype.validate = function (typeUnsafe) {
        return true;
    };
    return Wrapper;
}());
new Wrapper();

Historical Information
Version Reproduction Outputs
5.2.2

πŸ‘ Compiled
Emit:

"use strict";
var Wrapper = /** @class */ (function () {
    function Wrapper(validate) {
        if (validate === void 0) { validate = function (unsafe) { return this.validate(unsafe); }; }
        validate({});
    }
    Wrapper.prototype.validate = function (typeUnsafe) {
        return true;
    };
    return Wrapper;
}());
new Wrapper();

4.8.2, 4.9.3, 5.0.2, 5.1.3

πŸ‘ Compiled
Emit:

"use strict";
var Wrapper = /** @class */ (function () {
    function Wrapper(validate) {
        if (validate === void 0) { validate = function (unsafe) { return _this.validate(unsafe); }; }
        var _this = this;
        validate({});
    }
    Wrapper.prototype.validate = function (typeUnsafe) {
        return true;
    };
    return Wrapper;
}());
new Wrapper();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JS Emit The issue relates to the emission of JavaScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
4 participants