-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Compiler allows for using a value before it is imported #16166
Comments
Note that when I first encountered this issue, it was in code like this: // ...
import BAR = innerNamespace.BAR;
// ...
import {innerNamespace} from './bar';
// ... So it wasn't as obvious what was wrong. |
ES6 |
FWIW I was curious what babel emits for 'use strict';
var _bar = require('./bar');
console.log(_bar.BAR); |
That's interesting, thanks for the info! I guess changing the emit order would technically be a breaking change, since importing a module can have side effects. But I'm not sure how common this is in practice. |
Ideally we would hoist the imports, but not change the emit order. This is what already happens for AMD and SystemJS. |
I just found a similar surprise, and a place where TS should throw and doesn't: function useA() {
// using `a` before it is declared surprisingly doesn't error in either TS or the
// DevTools console
console.log(a);
}
useA(); // This does error at run time and should error at compile time
const a = 1;
useA(); |
It looks like this issue is scheduled to be closed soon with #39764 which tweaks import hoisting, but this fix will NOT fix the issue described by @appsforartists, which is the same bug I've experienced in my code. Are there plans to fix this error? |
I proposed a fix two years ago, but it got closed today. It never received any piece of reviews. I must say that apparently TypeScript Team is not interested in fixing this issue. Please do not expect this issue to be fixed. 🥲 |
Unfortunately, surveying the landscape, there are definitely projects that have taken a hard dependency on the current behavior and would by broken by any fix to hoist the imports, e.g. https://github.com/microsoft/tsdoc/blob/b7bddc1ceb6b6759857c66ef3af6e5e95655fb8c/tsdoc/src/nodes/DocDeclarationReference.ts#L165 where the lines are reordered to prevent a circular dependency problem. Since writing |
@RyanCavanaugh |
@appsforartists that behavior is intentional. Many programs "look" like they will TDZ under static analysis, but don't at runtime, so it's not practical to flag those as errors |
It's interesting to see this example. For the case of tsdoc, commonjs specific syntax should be used. something like // Circular reference
const { TSDocEmitter } = require('../emitters/TSDocEmitter'); or // Circular reference
import _TSDocEmitter = require('../emitters/TSDocEmitter');
const { TSDocEmitter } = _TSDocEmitter; |
TypeScript Version: 2.3.4
Code
Expected behavior:
TypeScript should reject
foo.ts
, because it is referring toBAR
before it is initialized.Actual behavior:
tsc
raises no errors, and generates the followingfoo.js
file:This fails at runtime with a
TypeError
.The text was updated successfully, but these errors were encountered: