Fix symbol auto-import: prevent value symbols from being added to type-only imports #61895
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where symbols used in computed property access (
obj[symbol]) were incorrectly added to existing type-only imports instead of creating separate value imports.Problem
When auto-completing symbol properties on objects, TypeScript would incorrectly import symbols as types when there was already a type-only import from the same module:
Before (incorrect):
After (correct):
Root Cause
The bug was in the
tryAddToExistingImportfunction insrc/services/codefixes/importFixes.ts. The logic gave preference to adding symbols to existing type-only imports even when the symbol needed to be imported as a value (like symbols used in computed property access).Solution
Added a check to prevent value-only symbols (
addAsTypeOnly === AddAsTypeOnly.NotAllowed) from being added to type-only imports. When such a symbol is encountered, the function now skips the type-only import and either finds a suitable value import or creates a new one.Changes
tryAddToExistingImport: Added early continue when attempting to add value-only symbols to type-only importscompletionsSymbolTypeOnlyImportBug.tsto verify the fix and prevent regressionsThe fix is minimal and targeted - it only affects the specific case where a value symbol would incorrectly be added to a type-only import, preserving all other existing behavior including the correct addition of types to type-only imports.
Fixes #61894.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.