Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 18, 2025

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:

// exportsSymbol.ts
export const SYM_FOO_BAR = Symbol.for("foo.bar");
export interface ObjWithSym {
    [SYM_FOO_BAR]: any;
}

// usesSymbol.ts
import type { ObjWithSym } from "./exportsSymbol";
declare const thing: ObjWithSym;

function main() {
    thing[SYM_FOO_BAR] // Auto-completing this would incorrectly add SYM_FOO_BAR to the type-only import
}

Before (incorrect):

import type { ObjWithSym, SYM_FOO_BAR } from "./exportsSymbol";

After (correct):

import type { ObjWithSym } from "./exportsSymbol";
import { SYM_FOO_BAR } from "./exportsSymbol";

Root Cause

The bug was in the tryAddToExistingImport function in src/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

  • Fixed logic in tryAddToExistingImport: Added early continue when attempting to add value-only symbols to type-only imports
  • Added test case: completionsSymbolTypeOnlyImportBug.ts to verify the fix and prevent regressions

The 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.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Jun 18, 2025
Copilot AI and others added 2 commits June 18, 2025 18:05
…n computed property access

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
… type-only imports

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot AI changed the title [WIP] Symbol properties on objects are imported by type and not by value when auto-completed Fix symbol auto-import: prevent value symbols from being added to type-only imports Jun 18, 2025
Copilot AI requested a review from RyanCavanaugh June 18, 2025 18:15
@jakebailey jakebailey deleted the copilot/fix-61894 branch July 1, 2025 16:34
@microsoft microsoft locked as resolved and limited conversation to collaborators Jan 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Symbol properties on objects are imported by type and not by value when auto-completed

3 participants