Description
Bug Report
π Search Terms
instantiation expression optional chaining target
π Version & Regression Information
This feature has been introduced in 4.7. I tested 4.7.0-dev.20220528
β― Playground Link
Playground link with relevant code
π» Code
a?.b<c>.d
π Actual behavior
When targeting <=ES2019 it generates (a === null || a === void 0 ? void 0 : a.b).d
, while when targeting >=ES2020 it generates a?.b.d
.
They have different behaviors: in the ES2019 output it always reads .d
, while in the ES2020 output it only reads .d
if a
is not nullish.
π Expected behavior
They should have the same behavior.
The ES2020 behavior feels more useful, but "adding <c>
to an optional chain ends the optional context" (as wrapping it in parentheses does) would be ok too as long as it's consistent. You would then have to write a?.b<c>?.d
.
(Babel's implementation has the same different behavior depending on the target, so this is apparently something very easy to overlook while implementing instantiation expressions π¬)