Skip to content

Optional chaining with default parameter fails #36295

@eventualbuddha

Description

@eventualbuddha

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions