typescriptvisual-studio-codetsconfig

Prevent certain files from being imported in VS Code


In TypeScript, how can I prevent certain files from being imported?

More specifically, VS Code will autosuggest import statements as you're typing. In my scenario, many of the files are generated and will never need to be imported. So as you're typing, VS Code will suggest all these extra files, making it difficult to find the actual source code files.

enter image description here

There are many ways (e.g., linting) to throw errors if certain files are ever imported, but I'm mainly interested in preventing the import suggestions. Typescript prevent imports from certain directory in project

Lastly, I know it's possible to disable import suggestions alltogether, but I'd still like to keep that functionality. How to disable automatic import statements in VsCode October 2017


Solution

  • You can now disable certain files from auto importing using typescript.preferences.autoImportFileExcludePatterns. This requires TypeScript 4.8 or greater.

    In your settings.json:

    {
      "typescript.preferences.autoImportFileExcludePatterns": [
        "./path/to/file/**/my-file.ts",
      ]
    }