microsoft / microsoft/TypeScript

Improve behavior of typescript.preferences.importModuleSpecifier: non-relative

Open
#45,499 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

⭐ Suggestion

Currently, with this setting"typescript.preferences.importModuleSpecifier": "non-relative" VScode goes to the tsconfig file and checks both baseUrl and paths and look for a match.

The situation that I experience is having a few paths in my path property and they sometimes overlap.
Example:

"baseUrl": ".",
    "paths": {
           "*": ["src/main/*"],
          "utilsFunctions/*": ["src/main/utils/utilsFunctions*"],
      },

With this configuration I can import a function from the utilsFunctions folder in one of these 2 options:

  1. import { foo } from "src/main/utils/utilsFunctions/functions.ts
  2. import { foo } from "utilsFunctions/functions.ts

Both of these import statements will work.

But If I add foo in my code without writing the import statement myself, vscode will auto-suggest me to use option no. 1, but In my case, I actually wanted option no.2 (in the above options).

Requested behavior:
When using foo (without it importing it first), VScode should suggest me both options.

My solution, in this case, was to reverse the order of paths like this:

    "paths": {
          "utilsFunctions/*": ["src/main/utils/utilsFunctions*"],
           "*": ["src/main/*"],
      },

🔍 Search Terms

importModuleSpecifier

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

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

Start by reproducing the overlapping tsconfig baseUrl and paths case with typescript.preferences.importModuleSpecifier set to non-relative. Trace how the import suggestion is selected and determine how both valid module specifiers should be presented; done means the requested alternatives are suggested without changing existing runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vscode
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.