-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Use before declaration false positive when combined with binding exports #14679
Comments
It is use before declaration. The error is valid. The export declaration does make use of the variable as there is an expected assignment. You are making assumptions about how the code is loaded in a loader. While in practice currently, a module loader may not run into a TDZ issue, in practice, the block scoped variable is being used before it is assigned, which is against the language spec. |
Indeed, note that there is no error when using |
@kitsonk no, that is literally how it is supposed to work according to spec. No evaluation should happen: If any evaluation does happen, it is a bug with your module loader. There is no assignment expected, at all. If you do need to convert it to commonjs or whatever is the native data structure for your module loader, you will need to either generate a getter, which is required anyway to correctly implement live binding and frozen module shape, or do a delayed assignment. |
@aluanhaddad it will work correctly, which is why it should not be a compile error. Even the code generated by the playground is (more-or-less) correct, but it will raise a compilation error anyway, even with |
Simple enough fix to not issue this error when the parent is an export declaration |
@Kovensky that's a great point, and I imagine it's one of the reasons that the register format uses Thank you for correcting me and for linking to the spec. |
Does this apply to classes? The current nightly ( export { XYZ }
// ~~~
// XYZ.ts(1,10): error TS2449: Class 'XYZ' used before its declaration.
class XYZ {
} |
@gcnew that should be valid too. Almost certainly the same root cause. Should be an easy PR if anyone's interested |
I haven't tried it, but it seems that @Kovensky's PR (#14700) takes care for classes as well. I just saw that he has commented that class exports were miraculously working prior to his change - #14700 (comment). |
Unfortunately, the issue still present if you compile with |
TypeScript Version: 2.2.1
Code
Expected behavior:
No error. The export declaration makes no use of the variable; what should happen is that
httpDelete
's TDZ should extend to thedelete
export, but that happens regardless of the relative position of theexport
declaration.Actual behavior:
Two errors in the same line about the variable being used before assignment.
This error is correct in the case of
export default
and the typescript-specificexport =
, but not in any other use ofexport
.The text was updated successfully, but these errors were encountered: