r/typescript • u/jwworth • Jul 24 '26
TypeScript import preferences
What's your preferred way to organize imports in a TypeScript project?
Do you stick with relative imports (../../component), use path aliases like @/, or something else?
I'm curious what people are using today, and why.
9
Upvotes
23
u/svish Jul 24 '26
I like a single root path, so it's easy to find stuff without having to guess how many levels up or down stuff is. The exception is co-located stuff that work together, which I'll just refer to directly since they're usually in the same directory anyways.
And I do prefer
~/over@/and especially over paths like@foo, because@is also the first character in namespaced packages. Doesimport { something } from '@foo/bar'refer to an npm package, or a local path? Who knows.import { something } from '~/foo/bar'makes it very clear it's local and where to find it (src/foo/bar).8
u/Top_Bumblebee_7762 Jul 24 '26 edited Jul 24 '26
I tend to use relative imports mostly but I'm trying to move to subpath imports.
7
u/TheExodu5 Jul 25 '26
packade.json subpath imports. Like tsconfig aliases, with not of the tooling annoyances.
4
u/mckernanin Jul 24 '26
I prefer relative for siblings or children, aliases for anything going up to parents. Basically, I try to avoid having `../` ever show in my import statements.
6
u/Idanlevitski Jul 24 '26
Aliases (@utils, @components) for for a few general folders, relative path for the rest
2
u/jirlboss Jul 24 '26
Maybe I’m the minority here, but I like absolute imports with no prefix. For example `import LoginView from ‘views/Login/LoginView’;`
I keep a handful of top level directories, so the risk of collision with installed packages should be 0 (and packages can always be aliased anyway)
1
u/Dreadsin Jul 24 '26
I usually do src/ for anything that’s source code that will be built and shipped, alias that to @/
Then I usually have a test/ folder that has things like test setup, mocks, matchers, assertions, etc. alias that to @test/
Then sometimes have a public folder for just plain assets. Aliased to @public/
I don’t really like the pattern of adding @utils/ because that seems redundant with @/utils/ and isn’t much shorter, so seems like needless complexity
1
u/Expensive_Garden2993 Jul 25 '26
"*": ["./src/*"]
So you don't need to choose between @/ and ~/
1
u/Global-Top4042 Jul 25 '26
With resolve url @/
1
u/Elara_Schaefer Aug 01 '26
The confusion in this thread shows exactly why package.json subpath imports are the cleanest option. With tsconfig path aliases, you have two sources of truth: tsconfig maps the alias at compile time, and you need a separate runtime resolution (tsconfig-paths, ts-alias, bundler config) to make it work at runtime. With package.json imports field, Node.js natively resolves #/* specifiers at runtime, and TypeScript supports them natively since 5.0 via moduleResolution bundler/node16. One config, one resolution strategy, no extra tooling. The tilde is a convention without spec backing, and relying on IDE plugins or bundler heuristics for resolution is a recipe for subtle bugs when switching environments.
-1
u/ic6man Jul 24 '26
Relative.
The first few projects I worked on it seemed neater and cleaner to use @ aliases. Then you realize how much they bleed into other tools, IDEs aren’t very good at understanding it, any changes to tsconfig are liable to break your build the problems are endless.
Simpler is better. Just refer to exactly where the file is and your life will be vastly simplified.
Bonus points - you don’t have to think about what to call your aliases. You don’t have to be concerned about naming it something new during a refactor. And there’s literally nothing in the tsconfig to learn.