Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Auto import option to make imports relative only within current directory #45015

Open
5 tasks done
JordanLongstaff opened this issue Jul 13, 2021 · 2 comments
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@JordanLongstaff
Copy link

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.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jul 13, 2021
@akomm
Copy link

akomm commented Jan 20, 2022

This is a quite underrated proposal. Could prevent issues like this:
remix-run/remix#1580

Most problems I try to avoid come from "../" imports. While in the same directory, "./" imports are fine.

I think this behavior even correlates with the way how namespace resolution in languages usually work. You either give an absolute namespace, or you give a relative one, narrowing down but not up.

Name proposal for the option: non-relative-parents (this totally makes sense I know)

@AsemK
Copy link

AsemK commented Dec 14, 2024

I would love this option. None of the other options of importModuleSpecifier is suitable for me right now.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants