Repository navigation
Exclude specific files from auto import suggestions #35395
Description
Search Terms
autoimport, exclude, ignore, isolation, encapsulation
Suggestion
We need a way to exclude files from auto import. Not from the project, only from the auto import suggestions.
Use Cases
My project includes a .d.ts file (generated from the previous version of the project) with legacy data structures (interfaces), many of which have the same names as their counterparts, newer data structures. As that file uses declare module "..." { ... } syntax, imports from it are non-relative, which is why auto import gives them a higher priority whereas modules with the newer structures have relative paths. The result is that the 'Add all missing imports' command constantly creates lots of wrong imports. If only there were a way to suppress suggestions for that specific module. I want to import from it only in rare cases.
I feel like this problem kind of defeats one of the important points of modules: I wanted to isolate those legacy definitions inside their module and forget about them, but because of this auto import issue they constantly get in the way.
Another example. Type definitions for libraries are in no hurry to use the new asserts feature of TS 3.7 for compatibility reasons. When I defined my own assert function to benefit from this feature, VS Code started to insist on importing assert form Node's console module instead, so, for lack of a better solution, I ended up renaming the function.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Activity
raijinsetsu commentedon Aug 7, 2020
We have a similar issue in a large project: a legacy set of code that no new code should reference but which must stay where it is for compatibility. Being able to instruct VSCode to ignore the files in that folder for the purposes of auto-import would be VERY beneficial.
Currently, we're looking into whether or not ESLint can at least cause an error but I'm not sure it supports restricting relative modules like that.
SimpleCookie commentedon Aug 10, 2020
I would also love to be able to exclude certain files. In my case I need a certain CSS file included AFTER another, however the organizeImports config automatically sorts them in the wrong order.
yuval-gilboa commentedon Aug 28, 2020
An additional use case we have is to enable splitting implementation of a large namespace to multiple module files but still expose it under a single namespace. This would allow us to have better control over shape of exposed apis without being tied to the internal module structure we use.
For example, assuming we have three module files:
module1module2module3We then define a re-exports module like this:
We then use the
libnamespace to access all members of module1...3. But the members of these internal modules are also exposed on the global 'namespace' which pollutes the api surface. This cannot be avoided due to the way the module system works, but if we were able to exclude these files from intellisense auto-import it would make them practically hidden and thus partially solve our problem.uhyo commentedon Aug 28, 2020
I also would love to see this feature.
I recently created a library named rocon and it has
react.d.tsat the root directory so that users can import stuff from"rocon/react". Thereact.d.tsreexports everything from./lib/react.The problem is that TypeScript's auto-import suggestion prioritizes importing from
rocon/lib/reactrather thanrocon/react. So it would be nice to exclude files insliderocon/libfrom the suggestion.IanEdington commentedon Jan 25, 2021
Ryan Cavanaugh (@RyanCavanaugh) It looks like this ticket generated quite a bit of feedback since you added that label. Is there any other specific feedback you're looking for?
RyanCavanaugh commentedon Jan 26, 2021
I liked the prior comment's example of a real-life file layout that caused problems.
richardbarrell-calvium commentedon Feb 17, 2021
I have a different use-case for asking for the same (or perhaps a different, but very similar) feature.
I have a codebase which uses a couple of libraries which have thousands of types defined in them (e.g. the "aws-sdk" library has, by my count, 28MB of ".d.ts" files). These libraries are only used in a couple of places in this codebase. It appears that their entire contents get considered for auto-suggestion everywhere. I believe this makes IDEs misbehave (I see VS Code spends >500ms in
JSON.parse(), I'm told that IntelliJ exhibits weird behaviour) when typescript sends the IDE a big auto-complete candidate list.Since I only use those libraries directly in a handful of files, it would be very useful to be able to omit them from typescript's auto suggestions. It would be even more useful to be able to turn auto suggest for them back on in just the specific files where I do want to use them.
As a workaround, I'm considering splitting my project into multiple packages (in one repo) so that the main top-level package doesn't have a direct dependency on the big libraries & their types aren't visible in it.
richardbarrell-calvium commentedon Feb 17, 2021
Issue #32174 looks like it's describing the same performance problem that I'm having & I'm looking for a way to work around. (FWIW I personally don't view it as a bug: expecting an analysis tool to be fast when feeding it 28MB of .d.ts files seems unreasonable to me, so I'd be ecstatic with a workaround for it rather than a "fix".)
alarbada commentedon Mar 13, 2021
Disabling autoimports with "typescript.suggest.autoImports" does fine for me, as the
Quick fixfunctionality does the auto importing well enough, even though it would be nice to be able to exclude some folder paths to improve perfomance.Glandos commentedon Jun 28, 2021
I have a Vue application with a
main.ts:I am using a code action on save to organize imports automatically. On this very file, I have:
As you can see, the import order is now wrong, since
authis imported beforeaxios.Organize Imports is a very good features, especially on save, but I need to exclude this specific file from it. Even an inline comment could do the trick.
posva commentedon Aug 11, 2021
This would be really useful for Vue as some libraries use an intermediate package in order to support Vue 2 and Vue 3 at the same time. The package is named
vue-demiand is used by the libraries, but never in the application. So when people are using libraries usingvue-demi(which points to either Vue 3 or Vue 2 depending on the installed dependencies), the auto-import features automatically addsimport { ref } from 'vue-demi'instead of the correspondingimport { ref } from 'vue'orimport { ref } from '@vue/composition-api'. This is very easy to overlook and can cause very subtle and hard to debug issues.Given the existing options of
typescript.preferences.import...and some others, maybe there could be an optiontypescript.preferences.excludeAutoImports: string[]that exclude paths and package names. Maybe even a glob if that even makes sense but I think ignoring a whole folder would be enough.fgarcia commentedon Aug 16, 2021
Maybe the
exportskeyword frompackages.jsonis not a propper standard yet despite being widely supported, but it could be used as guideline of the preferred import path.Whenever I want to import
{Logger}from my@app/core/logspackage within{repo}/packages/coreI am suggested imports fromWould it be so strange looking into
/packages/core/package.jsonand guess I just want this?Currently it is not even suggested. I would even argue that any relative import that pass by a folder with a
package.jsonfile should be removed from the list of suggestions.vincaslt commentedon Sep 10, 2021
@chakra-ui/reactis facing the same issue, it's reexporting everything, but autocomplete prefers direct imports from@chakra-ui/buttonetc.44 remaining items
andrewbranch commentedon Mar 7, 2023
It does. You just have to specify filenames or globs that cover the file(s) where those ambient modules are declared (whatever shows up in Go To Definition on the module specifier).
Every time someone proposes a very reasonable sounding change to auto-import heuristics, we find that implementing it makes the experience worse for ~50% of people. When “local” modules are given priority, people in really large codebases get mad that
Componentgets auto-imported from"../../../../../apps/email-templating/external/__mocks/types/flux-capacitor"when they obviously meant to import from"react". It can definitely be worth revisiting these things from time to time, but my preference is to come up with solutions that don’t make zero-sum trades in sorting/priority.0x80 commentedon Mar 8, 2023
Andrew Branch (@andrewbranch) Awesome. For me, adding
@types/node/assert.d.tsand@types/node/console.d.tsfixed my problem 👍0x80 commentedon Mar 18, 2023
... but somehow it doesn't work for "log" coming from the debug module. Vscode points to
"@types/node/debug/index.d.ts"but including it in the list of exclude patterns has no effect for me.andrewbranch commentedon Mar 20, 2023
That file doesn’t exist in @types/node, so I’m not sure what you’re seeing.
0x80 commentedon Apr 2, 2023
Andrew Branch (@andrewbranch) I got confused. It pointed to @types/debug, an external dependency. Everything seems to work fine after all 🎉
oviniciusfeitosa commentedon Sep 20, 2023
settings.jsonfile:NazmusSayad commentedon Jan 16, 2024
I wanna exclude all the import suggestions from "data-fns".
{ "typescript.preferences.autoImportFileExcludePatterns": [ "date-fns", "**/date-fns", "**/date-fns/**", "**/date-fns/**/*", "node_modules/date-fns", "**/node_modules/date-fns", "**/node_modules/date-fns/**", "**/node_modules/date-fns/**/*" ], "javascript.preferences.autoImportFileExcludePatterns": [ "date-fns", "**/date-fns", "**/date-fns/**", "**/date-fns/**/*", "node_modules/date-fns", "**/node_modules/date-fns", "**/node_modules/date-fns/**", "**/node_modules/date-fns/**/*" ] }I have tried these but still not working.
Typescript version: 5.3.2
VSCode version: 1.85.1
OS: Windows
andrewbranch commentedon Jan 16, 2024
Currently only workspace-relative paths (starting with
./or../) work on Windows. A fix is in progress.NazmusSayad commentedon Jan 16, 2024
Got it....
After using:
./**/node_modules/date-fnsIt's working
ev45ive commentedon Feb 6, 2024
Andrew Branch (@andrewbranch) This is great work. Very helpful. I can't figure out excluding built-ins though ?
typing
logi getimport {log} from "console"andconsole.log($0)snippet is next option, where import jest gets in the wayalso typing
beforeEachi getimport {beforeEach} from "node:test"when I have vitest or jest already in importsnon of these work:
testing on Windows 10
VSCode 1.85.2
"typescript": "^5.2.2"
andrewbranch commentedon Feb 6, 2024
./**/node_modules/@types/nodeshould do it. Also, leading with./shouldn’t be necessary by now if you’re using TypeScript nightly or VS Code Insiders.188599 commentedon Sep 24, 2024
I'm working on an Ionic using Angular's standalone components and they give you the following settings on a newly generated project:
But it doesn't seem like it does what is intended, because firstly, the first entry
@ionic/angular/commonis completely useless, since every single import under@ionic/angularis excluded, including the imports from@ionic/angular/standalonewhich you'd expect to be allowed, since it's where all the imports you need from their package reside.I also found the option
typescript.preferences.autoImportSpecifierExcludeRegexes, which as per it's description:should allow me to exclude the other imports while maintaining
@ionic/angular/standalonethat I need, but it does not seem to work, at all.andrewbranch commentedon Sep 24, 2024
Can you open a new issue following the template with repro steps?
188599 commentedon Sep 24, 2024
As soon as I said that here I saw that there was a new VS Code update, turns out that fixed the issue immediately for
typescript.preferences.autoImportSpecifierExcludeRegexes. I was gonna say that here but I couldn't find where I had posted anymore 😅applemate commentedon May 17, 2025
I also have a suggestion entry I like to be gone forever. Is there way to remove it ?