-
Notifications
You must be signed in to change notification settings - Fork 12.8k
VSCode mixes import and require syntaxes on auto-import #29038
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
Comments
Tested with import moment = require('moment');
moment() |
If |
Is that the ideal behavior, or current behavior? |
Ideal, I believe |
I have this issue too, but I don't have |
Any solution? |
This is starting to drive me insane. Why is this even breaking like this? If a file has import statements, shouldn't the default auto-import behaviour stick to that? |
…terop being on - fixes microsoft#29038
You’re right. I think this particular scenario was left untouched because it was somewhat contentious—moment has a CommonJS export, and your particular settings allow neither a CommonJS import ( |
@andrewbranch Interestingly, if a file already has an EDIT: just tested with VSCode Insiders, and it's still happening
tsconfig.json:
Typescript version: 3.7.5 |
@SBRK I can’t reproduce this. The version of VS Code shouldn’t matter; only the version of TypeScript does. Can you use this extension to use the TS nightly and share either a repo or exact steps and file contents (including package.json) that trigger this behavior? |
Same problem with TS nightly. I will try to make a simple repro |
Hmm, I can't seem to get a simple repro workspace. Intellisense doesn't offer me to auto import |
Ok I figured it out. It won't work unless it imports a file that itself imports @andrewbranch here: |
Further cases that don't offer an autocomplete as expected (but that might be digression and need its own issue):
|
This suddenly started to happen in my project without any modifications to my configuration. Instead of importing via |
@derwaldgeist you’d have to give us a lot more detail about exactly how to reproduce your problem. |
Hi, this is happening to me with Enzyme. Instead of importing via import, I now get require statements. |
@Marianovh do you have a tsconfig.json or jsconfig.json file? Since this file doesn’t already have any imports or exports, and there’s (presumably) no config file that tells us that it can use ES6 imports, and the typings for enzyme didn’t indicate that the importing file specifically needed to use ES6 or CJS imports, we just had nothing to go on and defaulted to CJS. We could potentially look at more files in your program as a better heuristic. But an immediate fix would be to add a jsconfig.json file at the root of your repo with {
"compilerOptions": {
"module": "es2015"
}
} |
Also, it should be noted that in any file that already has an ES6 import or export, it should do the right thing on future auto-imports in that file. If that’s not your experience, it’s a bug. |
@andrewbranch Thanks for your answer!
This is what happened, so it should not be a bug. Anyway, thank you very much for your help. |
Not a great one. |
To be honest, I don't have a clue what kind of information I could provide, but maybe this is just the wrong project to address this to (I came here via a Google search), since my problem is not directly related to TypeScript. I think, it's more a problem with the auto-import add-on itself. Sorry for bothering. |
@derwaldgeist There’s no auto-import add-on; JavaScript support in VS Code is implemented by TypeScript. But it matters a lot what you were importing, what imports (if any) the file already imported, whether you have a tsconfig/jsconfig file in your project, and if so, what settings it has. I’m guessing your scenario was similar to @Marianovh’s, where your file didn’t have any imports/exports/requires yet. I don’t think it’s very expensive for us to examine other files in your project to get a better idea for a default. That would have fixed @Marianovh’s issue without needing to add a config file. I’ll reopen this issue to explore a solution like that. |
@andrewbranch Thanks for the clarification. Well, at least for me, the biggest confusion was that everything worked perfectly - until all of a sudden auto-imports switched to require() statements instead of import statements. I never used any |
Same here. I not using Typescript nor have any config file, but after recent updates, auto import creates require statements instead ES6 import.
|
In this particular instance I encountered the bug with moment.js.
Steps to Reproduce:
Does this issue occur when all extensions are disabled?: Yes
Above recording is on fresh Insiders install with extensions disabled and being reproduced in a fresh project with just React and moment installed through yarn.
The text was updated successfully, but these errors were encountered: