DEV Community

Willochs Ojigbo
Willochs Ojigbo

Posted on

Enhancing Your Existing ReactJS Application with TypeScript and Absolute Imports

Introduction

It becomes increasingly important to implement new tools and practices as your ReactJS application becomes more complicated in order to enhance the quality and maintainability of the code. Static typing and cutting-edge tooling are features of TypeScript, a typed superset of JavaScript that can increase developer productivity. This post will walk you through setting up absolute imports for a simpler import syntax and integrating TypeScript into your currently running ReactJS application.

Prerequisites

Before we get started, ensure that you have Node.js and npm (Node Package Manager) installed on your machine. Additionally, make sure you have an existing ReactJS application ready for TypeScript integration.

Step 1: Create a ReactJS Application

If you haven't already created a ReactJS application, you can use a tool like Create React App to set up a new project. Open your terminal and run the following command:

npx create-react-app my-app
Enter fullscreen mode Exit fullscreen mode

This will create a new ReactJS project in a directory named my-app.

Step 2: Install TypeScript

Once your ReactJS project is set up, navigate to the project's root directory using the terminal:

cd my-app
Enter fullscreen mode Exit fullscreen mode

To install TypeScript, run the following command:

npm install --save typescript
Enter fullscreen mode Exit fullscreen mode

This will install TypeScript as a project dependency.

Step 3: Configuration

Next, you need to configure TypeScript for your project. In the project's root directory, create a new file named tsconfig.json. Copy and paste the following code into tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@components/*": ["components/*"],
      "@layouts/*": ["layouts/*"],
      "@pages/*": ["pages/*"]
    },
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve"
  },
  "include": ["src"]
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Rename Files

To allow TypeScript to recognize your existing JavaScript files, you need to rename them to use the .tsx file extension. For example, if you have a src/App.js file, rename it to src/App.tsx. This step is necessary for TypeScript to analyze and type-check your React components.

Step 5: Start the Development Server

Now, you can start the development server and begin working with TypeScript in your ReactJS application. Run the following command in the project's root directory:

npm start
Enter fullscreen mode Exit fullscreen mode

With TypeScript and the tsconfig.json file set up, you can take advantage of absolute imports to simplify your import statements. Instead of using relative paths like "../../components/Button", you can use aliases defined in the tsconfig.json file.

import React from 'react';
import Button from 'components/Button';
import HomePage from 'pages/HomePage';
import Layout from 'layouts/Header';

const App = () => {
  return (
    <div>
      <Header />
      <HomePage />
      <Button />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

๐Ÿ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someoneโ€™s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay