microsoft / microsoft/TypeScript

Auto importing should choose the nearest one where same symbol name is exported from multiple files

Open
#45,038 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Auto importing should choose the nearest one where same symbol name is exported from multiple files

🔍 Search Terms

auto import
same symbol name

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

💻 Use Cases

When .vscode/settings.json "typescript.preferences.importModuleSpecifier": "shortest",
The following auto importing behavior is very strange:

// src/app1/config.ts
export const LogLevel=1

// src/app1/main.ts
import {LogLevel} from "src/app2/config"
console.log(LogLevel)

// src/app2/config.ts
export const LogLevel=2

// src/app2/main.ts
import {LogLevel} from "src/app1/config"
console.log(LogLevel)

I think it should auto imports the nearest one, but it auto imports the other one.

When .vscode/settings.json "typescript.preferences.importModuleSpecifier": "relative",
The auto importing behavior is OK:

// src/app1/config.ts
export const LogLevel=1

// src/app1/main.ts
import {LogLevel} from "./config"
console.log(LogLevel)

// src/app2/config.ts
export const LogLevel=2

// src/app2/main.ts
import {LogLevel} from "./config"
console.log(LogLevel)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the examples using .vscode/settings.json with importModuleSpecifier set to shortest and relative. Compare the auto-import choices for the two app1/app2 config.ts files, then trace the TypeScript auto-import behavior that ranks candidates; done means shortest mode selects the nearest exported symbol in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.