TSConfig path aliases are a powerful tool that can help you to improve the readability, maintainability, and error-proofing of your TypeScript code. This allows for easier to read clean code, and enables us to move files around without having to update import paths in every file.
This is a huge time saver.
What Are Path Aliases?
The goal is to replace the following import statements:
with these import statements:
We can do this with a tsconfig.json
or jsconfig.json
file. We will be using TypeScript in this post.
How To Setup Path Aliases
Adding Path Aliases To Your TSConfig File
We need to update our tsconfig.json
file to enable aliases. We will add a paths
and baseUrl
property to the compilerOptions
object. Each path is relative to the baseUrl
.
This will tell TypeScript to replace the alias with the actual path when compiling the code.
Using Path Aliases In Your Code
Now in all of your source files, you can import components like this:
INFO
Frameworks like Astro and Next.js ship with built-in typescript support, although you may have to create the file tsconfig.json
. Consult your framework’s documentation for more information.
Why Should I Do This?
Let’s say we have the following file structure:
Annoying Relative Imports
If we want to import Hero.tsx
and Footer.tsx
into index.tsx
and solutions.tsx
, we would need the following import statements:
Refactoring Relative Imports
Now lets say we want to refactor. We now have multiple “solutions” and want to have each on their own page, and have them under a solutions
directory. The file structure now looks like:
Now we have to update the import paths of solution.tsx
:
You can see how this makes refactoring more of a chore. We have to update the import paths in every file that imports these components. This is a huge time sink and can lead to bugs if you forget to update the import paths.
Alias Imports Version
Alternatively, if from the start we were using aliases, we would not have to update any files using the components. This is far better for maintainability:
INFO
With this method, every file that needs to import these components will import them the same way. This makes it easier to move files around without having to update import paths.
Đăng ký bản tin
Theo dõi để nhận các bài viết mới nhất.
Không spam. Hủy theo dõi bất cứ lúc nào.
Additional Paths
This can be extended to any number of path aliases. Some other potential ones you might use:
TIP
When updating tsconfig.json
, you may need to restart your editor for the changes to take effect.