microsoft / microsoft/TypeScript
Auto import option to make imports relative only within current directory
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Suggestion
🔍 Search Terms
import relative
✅ 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.
⭐ Suggestion
An option added to the importModuleSpecifier preference to make imports absolute if and only if their relative path would begin with ../.
📃 Motivating Example
At my workplace, our least favourite relative imports are the ones that venture out of the current directory (i.e. they begin with any number of ../). But there are times where we want relative imports within the current directory (i.e. beginning with ./). I don't see any existing options that support this exact style of import, including project-relative.
In general, the motivation for not using relative imports beginning with ../ is that it makes moving code files around more difficult.
💻 Use Cases
shortest and relative are obviously not correct since they have no qualms about this issue.
non-relative is my current workaround, which only adds absolute imports.
project-relative I tried, but based on what I discovered and my understanding of the description in the tooltip, my guess is that it only adds absolute imports for files that are located outside of the workspace folder.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the language-service handling for the importModuleSpecifier preference and tracing how it chooses relative versus absolute module specifiers. Check existing tests for auto-import preferences, then add coverage showing that paths beginning with ../ use absolute imports while imports within the current directory remain relative.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100