Developer Guides

Typescript Setup with an Optimal TSConfig in Your Project

Learn step-by-step how to set up Typescript in your project with a minimal TSConfig and ready-to-use compiler configuration.

4 min read

Learn how to set up TypeScript in your project with a minimal tsconfig.json, ensuring your development environment is ready for strong typing and improved IntelliSense. This article walks you through the process step-by-step, from prerequisites to renaming files and using TypeScript effectively in your codebase.

Prerequisites for Setting Up TypeScript

Make sure you meet the following requirements before starting:

prerequisites

  • A working project environment (e.g., Next.js).
  • Node.js and NPM installed on your system.
  • Basic familiarity with JavaScript or Next.js.
  • A running development server to verify changes.

Creating a TSConfig File and Restarting Your Dev Server

To initialize TypeScript, the first step is creating a tsconfig.json file and restarting your development server.

steps

  1. Create a tsconfig.json file in the root of your project:

    bash
    touch tsconfig.json
  2. If your development server is already running, stop it:

    bash
    npm run stop
  3. Restart the server:

    bash
    npm run dev

    At this point, Next.js will detect the empty tsconfig.json file and show an error prompting you to install TypeScript dependencies.

Installing TypeScript Dependencies Automatically

Follow the system's instructions to install necessary TypeScript dependencies.

Installing TypeScript and related dependencies

npm install --save-dev typescript @types/node @types/react
# npm WARN save-dev The following devDependencies
# added: typescript, @types/node, @types/react

Once the installation is complete, restart the development server:

Restarting development server

npm run dev
# Ready on http://localhost:3000

After this step, Next.js will use the tsconfig.json file and set up the recommended default settings for your project.

Configuring Minimal TSConfig and File Inclusion

Out of the box, the TypeScript compiler will use safe defaults, so you don’t need to modify the configuration file immediately.

Renaming Files to Match TypeScript Standards

To fully utilize TypeScript, update your file extensions as necessary.

steps

  1. Identify files that include JSX or TSX elements and rename them to .tsx. For example:

    bash
    mv pages/index.jsx pages/index.tsx
  2. Rename standard JavaScript files to .ts:

    bash
    mv lib/utils.js lib/utils.ts
  3. TypeScript will automatically pick up these renamed files based on the tsconfig.json settings.

Utilizing Imports and Strong Typing from Next.js

Once TypeScript is active, you can explicitly import types and interfaces from Next.js to improve code structure and robustness.

For example, in a React component file:

tsx
import type { NextPage } from 'next';

const HomePage: NextPage = () => {
  return <h1>Welcome to TypeScript!</h1>;
};

export default HomePage;

This ensures strong typing and better IntelliSense as your project grows in complexity.

FAQ

What does a minimal tsconfig.json look like for a Next.js project?

A minimal tsconfig.json can be entirely empty when starting. Next.js automatically populates it with default settings like including .ts and .tsx files and adding helper files.

Do I need to manually add TypeScript types for Next.js?

No, Next.js automatically includes type definitions such as next-env.d.ts to help your editor provide IntelliSense and error checking. These types are managed as part of your dependencies.

How do I rename .js files in my project to use with TypeScript?

Rename .js files to .ts and .jsx files to .tsx (if they include JSX). TypeScript will pick them up automatically as long as the file paths match your project's tsconfig.json configuration.


Official reference: TypeScript handbook.