question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Import module using alias as path if there is one

See original GitHub issue

Suggestion

I want to request the option to auto-import typescript files in vscode using the alias as specified at paths in tsconfig.json, otherwise use a relative path. The related setting is typescript.preferences.importModuleSpecifier.

None of the options works this way. The closest is project-relative, but it only works if all aliases are outside the project. I want that all paths that satisfy an alias to use the alias, but without having to use absolute imports for other paths (those should be relative). The preferred way would be alias followed by relative.

Related: https://github.com/microsoft/vscode/issues/138067 (VSCode feature request; I was asked to create the Feature Request here)

Thanks.

🔍 Search Terms

Autoimport

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:17
  • Comments:5

github_iconTop GitHub Comments

2reactions
lucasbasquerottocommented, Feb 16, 2022

Just one thing to consider, is that an import in a file inside the path used in an alias should use relative path if the imported file is also in the path described by the same alias.

For example, /src/lib/file1 has an import to /src/lib/file2, and there’s an alias defining @/lib/ pointing to /src/lib. In this case, the import should be import ... from './file2' (otherwise it may end up behaving almost the same as absolute imports).

One workaround while not having this feature is to use absolute paths (and possibly making an alias for the root path, for example @/src/ points to /src/, although that might not be viable for every project). One benefit is that, because all paths are absolute, no imports need to be changed in the file that was moved (but imports in the files that pointed to the moved file may need to be changed, although a search & replace all occurrences is easier because all imports will be the same, while relative imports to the same file may differ and should be handled on a case by case basis). To sum it up, this might be better than relative imports in several cases, excepts in those cases in which files are moved together (which would keep the relative imports unchanged), especially if a lot of files are moved together or some base directory is renamed.

That said, each project may have their own conventions, and having the option to use relative paths, except those specified in an alias, would be great.

0reactions
Shaglockcommented, Dec 13, 2022

Any news about this issue? I also would like that aliases specified at paths in tsconfig.json would be the first suggestion from VSCode, not the second.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Absolute Imports and Module Path Aliases
Configure module path aliases that allow you to remap certain import paths. ... One useful feature of these options is that they integrate...
Read more >
Module Resolution or Import Alias: The Final Guide - Raul Melo
Module Resolution or Import Alias: The Final Guide. A guide how to enable this feature in (almost) any JS/TS project.
Read more >
Absolute imports and module path aliases are a game changer!
So we can make use of paths to declare module aliases. The configuration would look something like this: The import after configuring module...
Read more >
python - Import modules using an alias - Stack Overflow
Using import module as name does not create an alias. You misunderstood the import system. Importing does two things: Load the module into ......
Read more >
Import-Alias - PowerShell - Microsoft Learn
The Import-Alias cmdlet imports an alias list from a file. Beginning in Windows PowerShell 3.0, as a security feature, Import-Alias does not overwrite ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found