Feature: Import module using alias as path if there is one
See original GitHub issueSuggestion
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:
- Created 2 years ago
- Reactions:17
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 beimport ... 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.
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.