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

Configuration For Treating JS/TS Files as Independent Units (modules) #14279

Closed
mjbvz opened this issue Feb 24, 2017 · 23 comments · Fixed by #47495
Closed

Configuration For Treating JS/TS Files as Independent Units (modules) #14279

mjbvz opened this issue Feb 24, 2017 · 23 comments · Fixed by #47495
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Feb 24, 2017

From microsoft/vscode#16892

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code
For a JavaScript project

jsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "allowSyntheticDefaultImports": false
    }
}

a.js

var foo = 'test'

b.js

var foo = 2

Expected behavior:
There should be a way to configure TypeScript to treat a.js and b.js as isolated units. That way, foo would have the type as defined in its file

Actual behavior:
The files are treated as a single unit. foo either has a string or number type in both files.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 24, 2017

What do you mean single units? if these are modules, wold not they have at least one import or export? if they are not, why should they be "units"?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Feb 24, 2017
@mjbvz
Copy link
Contributor Author

mjbvz commented Feb 24, 2017

@mhegazy The request would be to treat all JavaScript files as modules by default, even if they do not follow the standard module conventions.

We've seen this type of issue few reported a few times, specifically when people are working on websites. Even though the JavaScript files may be be loaded on separate pages, as far as I know, there is no way to configure VScode to treat these files as independent modules, at least without making the files proper modules.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 25, 2017

@mhegazy Mainly this is annoying for when you're going to write a module eventually, but TypeScript keeps bugging you.

@mjbvz, while I agree that this is annoying, how do you plan to surface it in VS Code? Do you believe there are no people writing global-style code in VSC? Is it possible that a different group of people would report the opposite issue?

@mjbvz
Copy link
Contributor Author

mjbvz commented Mar 6, 2017

@DanielRosenwasser I would see this being something that users can opt into by enabling another compiler option in the jsconfig.json

See microsoft/vscode#22011 for another manifestation of this issue. Users either need a way to opt into isolated files in a JS project, or we need to improve TypeScript's handling of multiple definitions of a symbol across multiple JS files

@mhegazy mhegazy added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed Needs More Info The issue still hasn't been fully clarified labels Aug 29, 2017
@mikl
Copy link

mikl commented Sep 3, 2018

Is there any documentation on how to persuade TypeScript that a file is in fact a module? I have a file in a Node.js project where TS gives me these errors, despite the file having both imports and exports.

Edit: Nevermind, it was a strange issue in PhpStorm that was producing spurious errors from a file that turned out to be fine once PhpStorm was restarted.

@KostyaTretyak
Copy link

KostyaTretyak commented Apr 17, 2019

I like the idea with the config, but why only JavaScript files are mentioned here? What about TypeScript files?

@DanielRosenwasser DanielRosenwasser changed the title Configuration For Treating JS Files as Independent Units Configuration For Treating JS/TS Files as Independent Units (modules) Apr 17, 2019
@schl3ck
Copy link

schl3ck commented Sep 25, 2019

Is there any progress to this issue?

It has the label Awaiting More Feedback, but I don't see any open questions?

In my opinion the best way to surface this is to provide another compiler option like @mjbvz already suggested.

@JacksonKearl
Copy link

Hitting this with two files that run in separate processes. I cant re-use names between the two, but they aren't technically modules.

@phudson
Copy link

phudson commented Jul 7, 2020

I'm the author of the vscode issue @mjbvz refers to. I'd like to also request this feature - we have multiple JS files that run in the same application, use the same APIs, but are independent. I want a jsconfig.json to set options, but I do NOT want the files treated together, but independently.

@Gr3q
Copy link

Gr3q commented Oct 9, 2020

Hitting this as well, now I can't even use export {}; as a workaround after an update because it gets included with es6 module resolution.

I'm working in an environment what has custom module resolution and I can't flag my files as independent units anymore.

@phudson
Copy link

phudson commented Oct 9, 2020 via email

@redradist
Copy link

I also faced with such need ... currently TypeScript enforce developer to add export {}; but it is agly crutch

@trafnar
Copy link

trafnar commented Jul 22, 2021

I also have this problem, I'm creating a project which has a series of typescript files, each of which represents a "userscript" (greasemonkey / tampermonkey script), so each file is totally independent, and also, is not a module.

The fact that I want to have these files all within one vscode project means I can't use the same variable names in any of them.

@esc-mhild
Copy link

Here yet another use-case in which this problem occurs: We have built a data-processing platform based on the Graal runtime (runs Javascript, C, Java, Python, ... in the same memory space) and author data-processing scripts in Typescript. Namespacing is, of course, an option but seems unnecessarily pedantic if any one script is only ever run by itself (what other posts call "in isolation", i.e., as the only script within in the scope of the runtime).

As a work-around, a trip down Memory Lane might do the trick: Have your build tool enclose the script in an anonymous function that is executed immediately. In Grunt, for example, using grunt-contrib-concat and grunt-contrib-copy like this:

concat : {
	'ts-hack' : {
		options : {
			banner : '(function(){\n\n',
			footer:  '\n\n})();'
		},
		files : [ {
			expand : true,
			cwd : 'src/main/ts',
			src : [ '**/*.ts' ],
			dest : 'target/ts'
		}]
	}
},
copy : {
	'ts-hack' : {
		files : [ {
			expand : true,
			cwd : 'src/main/ts',
			src : [ '**', '!**/*.ts' ],
			dest : 'target/ts'
		} ]
	}
}

In tsconfig.json:

"include": [
	"target/ts/**/*.ts"
], ...

@phudson
Copy link

phudson commented Aug 10, 2021

Just got bitten by this AGAIN, where a function with the same name (and same functionality, as it happens) was defined in two files, one with a JSDoc for the parameters, one without (and so got typed as taking "any" parameter types).

An erroneous call to the function was written and not caught because VSCode found the "any" overload.

@phudson
Copy link

phudson commented Mar 11, 2022

Can you explain how #47495 fixes this issue? I can't see it right now but I may be being dense.

What do I set to have each JS file (just JS, no modules in sight at all) considered in isolation?

@weswigham
Copy link
Member

moduleDetection: force, optionally alongside isolatedModules: true.

@phudson
Copy link

phudson commented May 19, 2022

How do I understand when / in which version this will happen? In the latest vscode, and in the typescript online doc, there doesn't seem to be support for "moduleDetection: force" still.

@reporter123
Copy link

Looks like Typescript 4.7 will introduce it that's the merge I saw. That version is in beta.

@ValeTheVioletMote
Copy link

Is there any workaround? iirc having export {} flags it to the browser as a module, which requires it to be imported as one, etc.

I guess the only other option would be to downgrade TSC to 3.9? We didn't have this bug then.

@DanielRosenwasser
Copy link
Member

@ValeTheVioletMote I'm not sure what issues you might be having since the comment is sparse on details. If you're having an issue in a latest version of TS, it might be worth filing an issue with more detail; if not, a StackOverflow question might be a better direction.

@reporter123
Copy link

Typescript 4.7 appears to have been released now with the
"moduleDetection": "force" option.

Beyond that I don't think there's any other way.

@microsoft microsoft locked as resolved and limited conversation to collaborators Aug 2, 2022
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 2, 2022

You can read up on the the moduleDetection flag on our documentation. Most users coming to this issue are likely interested in the force option.

New issues should be filed separately.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.