Typescript: Configuration For Treating JS/TS Files as Independent Units (modules)

Created on 24 Feb 2017  Â·  11Comments  Â·  Source: microsoft/TypeScript



From https://github.com/Microsoft/vscode/issues/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.

Awaiting More Feedback Suggestion VS Code Tracked

Most helpful comment

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

See https://github.com/Microsoft/vscode/issues/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

All 11 comments

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 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.

@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?

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

See https://github.com/Microsoft/vscode/issues/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

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.

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

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.

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

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.

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.

"Custom module"? Pah, I don't even have modules. A set of independent
files is all I have. Which is why this would be so useful

On Fri, 9 Oct 2020 at 20:09, Attila Greguss notifications@github.com
wrote:

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.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/microsoft/TypeScript/issues/14279#issuecomment-706355774,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AADZSIPQWZQKAVHSF4CNQ3DSJ5NVXANCNFSM4DBK537A
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  Â·  3Comments

zhuravlikjb picture zhuravlikjb  Â·  3Comments

Antony-Jones picture Antony-Jones  Â·  3Comments

MartynasZilinskas picture MartynasZilinskas  Â·  3Comments

jbondc picture jbondc  Â·  3Comments