@/ imports - consider alternatives?
See original GitHub issueThank you for this amazing source of practices, I’ve been sharing it with lot of people!
@/
imports confuses me, could you please add alternatives to the doc? I’m not sure if and why this is a best way.
Another option is to set baseUrl: 'src'
to tsconfig and now instead of ‘@/components/X’ we import from ‘components/X’. I don’t think there are meaningful and maintained ‘components’, ‘hooks’, ‘lib’, ‘assets’ packages on npm.
Another option is to use relative paths, editors are smart nowadays, they write imports automatically, they update imports automatically when moving files. At least VSCode and JetBrains editors a capable. Most of people are using VSCode, so this shouldn’t be a problem.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Import substitution is making an unwelcome comeback | PIIE
Import substitution is the idea that blocking imports of manufactured goods can help an economy by increasing the demand for domestically ...
Read more >Import: Definition, Examples, and Pros and Cons - Investopedia
An import is a good or service bought in one country that was produced in another. Imports and exports are the components of...
Read more >Why Alternatives To Tariffs Can Be More Effective - NPR
Tariffs are a tax on imports, which is supposed to make imports more expensive and domestic goods at least relatively more appealing, right?...
Read more >A Secret Weapon for Doing Competitor and Supplier Research
If you've been importing long enough, you've probably found a competitor (probably on Amazon) selling the exact same product as you, but at...
Read more >Alternatives to Food Import Dependency - Global Policy Forum
There is a vast number of proposals, strategies and initiatives how to improve food security and agricultural production.
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
It works well with VSCode, you should definitely give it a shot.
And if you have the
import/order
enabled in eslint you will get a nice visual separation of local vs dependency modules which you do not get with the suggested approach.(ignore the squiggly line in the left image for now), VSCode did linting auto fix on save automatically and reordered the imports.
Hi there,
Both of your suggestions are valid. I wrote a bit here about the reason why I am choosing this way:
“It is also possible to define multiple paths for various folders(such as @components, @hooks, etc.), but using @/* works very well because it is short enough so there is no need to configure multiple paths and it differs from other dependency modules so there is no confusion in what comes from node_modules and what is our source folder. That means that anything in the src folder can be accessed via @, e.g some file that lives in src/components/MyComponent can be accessed using @/components/MyComponents.”
In a nutshell, I find it useful to have a symbol in front of it because it will prevent conflicts from
node_modules
imports in case a folder insrc
has the same name as some dependency - happens not very often, but it’s nice to be sure and not to think about it ever again. In the end, it’s just a matter of preference 😃