Skip to content

In JS, assignment to void 0 isn't a declaration #39452

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

Merged
merged 2 commits into from
Jul 6, 2020

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Jul 6, 2020

Previously, property assignments with void 0 initialisers were treated like any other values. But this causes us to choke when checking our own commonjs emit. This is something that happens by mistake a fair amount, so this PR goes back to treating these assignments as normal assignments.

This should allow us to check our own emit in loose cases without harming other code bases, since void 0 is rarely written by hand.

Note that other errors still happen: noImplicitAny forbids accessing undeclared properties on object literals, and strictNullChecks forbids assigning undefined to properties with a different type. However, this change is enough to unblock compilation with strictNullChecks: false.

Fixes #38552

Previously, property assignments with `void 0` initialisers were treated
like any other values. But this causes us to choke when checking our own
commonjs emit. This is something that happens by mistake a fair amount,
so this PR goes back to treating these assignments as normal
assignments.

This should allow us to check our own emit in loose cases without
harming other code bases, since `void 0` is rarely written by hand.

Note that other errors still happen: noImplicitAny forbids
accessing undeclared properties on object literals, and strictNullChecks
forbids assigning `undefined` to properties with a different type.
However, this change is enough to unblock compilation with
`strictNullChecks: false`.
@sandersn
Copy link
Member Author

sandersn commented Jul 6, 2020

@rachelgshaffer you might be interested in this as well.

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the strict mode case, we'd need to do something like calculate the type via control flow to handle eliding the undefined assignment from the final type, right?

@sandersn
Copy link
Member Author

sandersn commented Jul 6, 2020

@weswigham Control flow would be one way to do it. But we'd basically be lying about checking a type -- it would probably be easier to just skip the assignability check for lhs=export.* and rhs=void 0.

For the record, I don't think it's a good idea. This is basically an error-recovery feature, and I don't think disrupting assignability checks is worth it.

@sandersn sandersn merged commit 0d84f21 into master Jul 6, 2020
@sandersn sandersn deleted the js-assignment-to-voidzero-arent-declarations branch July 6, 2020 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript 3.9 CommonJS code emitted sets exports to void 0 (leading to error TS2323: Cannot redeclare exported variable)
3 participants