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

Top comments (0)