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

non-null assertion in typescript changes generated javascript #7791

Closed
hunkydoryrepair opened this issue Aug 9, 2023 · 2 comments · Fixed by #7202
Closed

non-null assertion in typescript changes generated javascript #7791

hunkydoryrepair opened this issue Aug 9, 2023 · 2 comments · Fixed by #7202
Labels
Milestone

Comments

@hunkydoryrepair
Copy link

hunkydoryrepair commented Aug 9, 2023

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

a?.b!.c ?? 0

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 to a?.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, while a?.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

@magic-akari
Copy link
Member

This issue will be fixed in #7202

@kdy1 kdy1 closed this as completed in 9c9a538 Sep 25, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.89 Sep 25, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 26, 2023

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants