-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Description
TypeScript Version: 3.7.2 & Nightly (2020-01-18)
Search Terms: optional chaining, default parameters, _a is not defined
Code
const a = (): { d: unknown } | undefined => undefined;
((b = a()?.d) => {})();Expected behavior:
This should run without error.
Actual behavior:
This throws an error: ReferenceError: _a is not defined
Playground Link: link
Related Issues: n/a
Here's the code it compiles to now for illustration:
"use strict";
const a = () => undefined;
((b = (_a = a()) === null || _a === void 0 ? void 0 : _a.d) => { var _a; })();This error happens because _a is used before it's defined. TypeScript adds a var _a; inside the function body, but because the assignment occurs in the parameter _a is not in scope and the assignment fails. I'm not sure what the preferred solution would be here. I solved it in decaffeinate by using a helper function, though I never really liked that solution.
lourd, fabiospampinato and defghy
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScript