TypeScript

React TypeScript: Simplify Imports with Path Aliases

Advertisements

As codebases grow larger and more complex in structure, imports can become unmanageable. As more directories are added, imports become intricately long and obscure clarity. Fortunately, we can simplify imports with path aliases.

The problem

The following is a fairly common occurrence in a large project:

import { ComponentButton } from '../../../../components/ComponentButton';
JavaScript

This can be burdensome to read, follow, and sometimes refactor.

The solution

Our code can be made more understandable and clearer by using path aliases to create import path shortcuts. Regardless of the size of the project, they allow you to obtain concise imports such as these:

import { ComponentButton } from '@components/ComponentButton';
JavaScript

Setup

In a TypeScript project, we can set path aliases in the tsconfig.json file located at the project’s root.

We’ll need to add a ‘paths’ attribute inside the ‘compilerOptions’ key in the tsconfig.json file:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"]
    }
  }
}
JSON

The configuration above tells the TypeScript compiler to interpret imports with “@components/” to correspond to the “src/comoponents/” directory.

We can then strategically expand the aliases as the application grows in size. Here’s an example of an updated section in the tsconfig.json that includes additional aliases:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@components/*": ["components/*"],
      "@shared/*": ["components/common/shared/*"],
      "@views/*": ["views/*"],
      "@hooks/*": ["hooks/*"],
      "@services/*": ["services/*"],
      "@utilities/*": ["utilities/*"]
    }
  }
}
JSON

In this example, the ‘baseUrl’ attribute simplifies the alias definitions, referencing the “src” directory as the root for all specified paths.

Conclusion

Path aliases may significantly increase the organization and readability of code in React and TypeScript apps. Developers can streamline their coding, manage imports more easily, and lower the chance of errors resulting from complex relative paths by creating obvious shortcuts for import paths. Let us know in the comments if you have any questions about path aliases.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

2 weeks ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

3 weeks ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

4 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

1 month ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

2 months ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

2 months ago
Advertisements