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

TypeScript definite assignment assertions are not removed for private class fields #639

Closed
elisee opened this issue Jul 24, 2021 · 3 comments · Fixed by #658
Closed

TypeScript definite assignment assertions are not removed for private class fields #639

elisee opened this issue Jul 24, 2021 · 3 comments · Fixed by #658

Comments

@elisee
Copy link

elisee commented Jul 24, 2021

Hi! Thanks for a great tool, it's been wonderful using it for instant TypeScript-to-JavaScript conversion in my projects.

The TypeScript transform should remove the ! definite assignment assertion for private class fields:

class A { #b!; }

The expected result is:

class A { #b; }

But instead, the ! is currently kept.

Example: https://sucrase.io/#selectedTransforms=typescript&code=class%20A%20%7B%20%23b!%3B%20%7D

@Rugvip
Copy link
Contributor

Rugvip commented Oct 1, 2021

Afaik that syntax is not valid in TypeScript, and this seems to compile alright:

class A { #b!: number }

@elisee
Copy link
Author

elisee commented Oct 10, 2021

My bad, I guess I went too far with minimizing the test case. Still, I believe this:

class A { #b!: number; }

Should compile down to this:

class A { #b; }

not this, which isn't valid JavaScript:

class A { #b!; }

@alangpierce
Copy link
Owner

Thanks for reporting! A related problem I found while investigating is that even non-private fields have problems here when using disableESTransforms: true. For example, this:

class A { b!: number; }

becomes this:

class A { b!; }

I think I have a fix working, will put up a PR soon.

alangpierce added a commit that referenced this issue Oct 18, 2021
Fixes #639

Previously, the `!` operator for class field declarations was not treated as a
type token, so it wasn't automatically removed at transpile. In most cases, this
wasn't relevant because the class field transform removes uninitialized fields
completely. However, there are two cases where it causes an issue:
* `disableESTransforms: true`, which disables the class field transform.
* Private fields, which are skipped by the class field transform.

In both cases, we can fix the issue by just setting the `!` as a type token so
that it will naturally get removed by the TS transformer.

I also did a little refactoring to pull out the logic for handling individual
type tokens.
alangpierce added a commit that referenced this issue Oct 18, 2021
Fixes #639

Previously, the `!` operator for class field declarations was not treated as a
type token, so it wasn't automatically removed at transpile. In most cases, this
wasn't relevant because the class field transform removes uninitialized fields
completely. However, there are two cases where it causes an issue:
* `disableESTransforms: true`, which disables the class field transform.
* Private fields, which are skipped by the class field transform.

In both cases, we can fix the issue by just setting the `!` as a type token so
that it will naturally get removed by the TS transformer.

I also did a little refactoring to pull out the logic for handling individual
type tokens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants