You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Describe the bug
Typescript documentation explicitly states that using the non-null assertion (
!
) does not change the emitted code.But, using
swc
, this isn't the case. The following create different outputs:a?.b!.c ?? 0
vs
a?.b.c ?? 0
At least, when targeting ES5.
The first case will generate something like:
(_a = (a === null || a === void 0 ? void 0 : a.b).x) !== null && _a !== void 0 ? _a : 0
while the second generates
(_a = a === null || a === void 0 ? void 0 : a.b.x) !== null && _a !== void 0 ? _a : 0
Using
tsc
, these expressions generate the same output.Works as expected in versions < 1.3.63
Input code
Config
n/a
Playground link
https://play.swc.rs/?version=1.3.75&code=H4sIAAAAAAAAA0u010tS1EtWsLdXMAAAPu9sgQwAAAA%3D&config=H4sIAAAAAAAAA1WPSw7CMAxE9z1F5DVbWHAHDmEFtwrKT7GRiKrenaRJCt3ZM2M%2Fe52UghdruKu1lKWJmJjS0ReFsxf8FAUkR2KdTBS4DFe4WjNapl3amgOCaSGpU8TXHgcbAtOId80Zb%2Bb8D9TBxUTM52CNol8snXFTR4ILz%2Fdu9kfqsQ1%2Fg19owI7FYPgxJtva7QvTya0RFQEAAA%3D%3D
Expected behavior
a?.b.c
runs identically toa?.b!.c
output
(_a = a === null || a === void 0 ? void 0 : a.b.x) !== null && _a !== void 0 ? _a : 0
Actual behavior
Using
a?.b!.c
throws an error at runtime when a is undefined, whilea?.b.c
does not.outputs
(_a = (a === null || a === void 0 ? void 0 : a.b).x) !== null && _a !== void 0 ? _a : 0
Version
1.3.63 - 1.3.75
Additional context
No response
The text was updated successfully, but these errors were encountered: