-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 can see objects globally if they are not exported. #31822
Comments
https://www.typescriptlang.org/docs/handbook/modules.html
|
@nattthebear Good to know. But, IMHO, it shouldn't apply in case of commonjs. |
Shouldn't apply to ESM either. A script executed as |
It's typescript's solution to the Michael Jackson problem. If a script has neither exports nor imports, tsc assumes that it's not part of whatever module system you've specified in your compiler options. You can see this on the playground which is set to AMD: The right side output pane is just plain script lines, until you add an https://www.typescriptlang.org/play/#src=import%20a%20from%20b I see how it can be an issue, but it's not that likely to come up: You'd need a module with no exports, so it was imported only for side effects, and no imports, so those side effects must only be on the global object and anything reachable from there. |
Even if you It doesn’t actually solve the Michael Jackson problem IMO because the syntactically-distinguishable modules proposal was rejected; the bare file will still be treated as a module at runtime if the host decides it wants to run code as modules, and this will be out of the user’s control. And at that point TypeScript’s idea of what your code is doing is different from what will actually happen at runtime. Hence the footgun. Would be nice if we could disable this, at least. |
@fatcerberus If you specify
Although |
From @weswigham in that same thread:
That error message makes no sense, though. Namespaces and modules are very different things—the documentation even goes out of its way to stress it IIRC Thanks though, I appreciate it. |
@fatcerberus I agree about the error message being horrible, but at least it's a compile time error for something that would end up a run-time error I am not sure about what the 'individual' part means though. I think that might refer just to the emit part not the type checking. From my testing imported modules are checked as expected with I would love some actual documentation on what |
Well this looks like bad news already: As for @weswigham Could you come here and explain exactly what |
@fatcerberus But I think that is just about emit phase. I think the emit can't depend on types in other files. Running the command line compiler does the type checking as expected, at least from what I tested: {
"compilerOptions": {
"isolatedModules": true,
"module": "commonjs"
}
} // a.ts
export class MyStuff {
x: string = "";
}
export function doStuff(cmd: MyStuff) { }
//b.ts
import { doStuff, MyStuff } from './a'
import { a } from "./aa" // error
doStuff(new MyStuff())
doStuff(""); // error
new MyStuff().x
new MyStuff().xx // error Output: b.ts:2:19 - error TS2307: Cannot find module './aa'.
2 import { a } from "./aa" // error
~~~~~~
b.ts:5:9 - error TS2345: Argument of type '""' is not assignable to parameter of type 'MyStuff'.
5 doStuff(""); // error
~~
b.ts:7:15 - error TS2339: Property 'xx' does not exist on type 'MyStuff'.
7 new MyStuff().xx // error |
connected to #18232 |
TypeScript Version: 3.5.1
Search Terms:
Code
File
./index.ts
File
./test.ts
File
./tsconfig.json
Command
Expected behavior:
TypeScript compiler should report an error in
./test.ts
thatMyPrivateClass
is not visible.Actual behavior:
TypeScript doesn't report any error and generates invalid
./test.js
and./index.js
files. For example,node ./test.js
are failing with this issue:Playground Link: No playground link because we need two files but here's GitHub repository https://github.com/ts-common/visibility-test/tree/d3c03a1cd4641da1bd1356e751e2cea10ea91025
Related Issues: I couldn't find any.
Note:
TypeScript compiler will give a compilation error only if the class is exporter:
The text was updated successfully, but these errors were encountered: