如何禁用Visual Studio 2017自动注入import语句?
Hey there, I’ve run into similar quirks with VS 2017’s JavaScript tooling before—let’s walk through how to stop those unwanted auto-injected lines when you use functions like clearTimeout.
Possible Causes
This is almost always triggered by either a third-party extension you’ve installed, or Visual Studio’s built-in JavaScript/TypeScript Language Service trying to "help" by adding what it thinks are missing dependencies.
Step-by-Step Solutions
Disable Suspect Extensions
Since you mentioned having several extensions installed, start by narrowing down which one might be causing this:- Go to
Tools > Extensions and Updates - Navigate to the Installed tab, filter for JavaScript-related extensions
- Disable one extension at a time, restart VS, then test by adding
clearTimeoutto your file - Repeat until the auto-injection stops—you’ve found the culprit (you can either keep it disabled or check for updates for that extension)
- Go to
Turn Off Auto-Import in Language Service Settings
VS 2017’s built-in JS tooling has auto-import features that might be misfiring for local files:- Open
Tools > Options > Text Editor > JavaScript/TypeScript > Language Service - Look for options like "Auto-import modules" or "Add missing imports"
- Uncheck these options, click OK, and restart VS to apply changes
- Open
Mark Files as Non-Module (if applicable)
If VS is treating your local JS file as a module (even if it’s not), it might try to inject dependencies. Add this comment at the very top of your file to tell VS to skip module-related auto-processing:// @ts-nocheck // @noflowAlternatively, remove any accidental
import/exportstatements that might be triggering module mode.Reset Visual Studio Settings
If all else fails, resetting your VS settings to default can eliminate misconfigured options:- Go to
Tools > Import and Export Settings - Select "Reset all settings", follow the prompts to back up your current settings first
- After resetting, test the issue again
- Go to
内容的提问来源于stack exchange,提问作者TheCuBeMan

