Table of contents
1.
Introduction
2.
What is tsconfig.json?
2.1.
Why Do You Need tsconfig.json?
2.2.
Setting Up Your First tsconfig.json
2.3.
Customizing Compiler Options
2.4.
Common Use-Cases
3.
Frequently Asked Questions
3.1.
Is tsconfig.json mandatory for a TypeScript project?
3.2.
Can I have multiple tsconfig.json files in a single project?
3.3.
What happens if I don't include the include and exclude properties?
4.
Conclusion
Last Updated: Feb 5, 2025

Setting Up tsconfig

Author Lekhika
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the world of TypeScript, the tsconfig.json file serves as the central hub of your project, much like the captain's wheel on a ship. This JSON configuration file allows you to control how the TypeScript compiler will convert your TypeScript code into JavaScript. But what exactly goes into this file? How can you tailor it to meet your project’s unique needs? 

Setting Up tsconfig

In this article, we'll demystify the tsconfig.json file by walking you through its most essential properties, how to set it up, and some common scenarios where custom configurations come in handy.

What is tsconfig.json?

Simply put, tsconfig.json is a configuration file that specifies the root files and compiler options required to compile a TypeScript project. It plays a pivotal role in shaping the behavior of your TypeScript compiler, guiding it on what to do and what not to do.

Why Do You Need tsconfig.json?

Compiler Options: It lets you set various compiler options like whether to allow JavaScript files or only TypeScript files, how to handle modules, etc.

Project Structure: It helps define your project structure by specifying which files to include or exclude during the compilation process.

Type Checking: You can enable or disable strict type checking and various other type-related settings.

Setting Up Your First tsconfig.json

Creating a tsconfig.json is straightforward. Simply navigate to your project’s root directory and run:

tsc --init

This command generates a tsconfig.json file populated with default values and extensive comments.

Basic Properties

Here are some of the most commonly used properties:

compilerOptions: An object containing key-value pairs for various compiler options.

include: An array specifying which files should be included in the compilation.

exclude: An array specifying which files should be excluded from the compilation.

A Sample tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

Customizing Compiler Options

The compilerOptions object in tsconfig.json is where the real action happens. Here are some of the most impactful properties you can set:

target

Specifies the ECMAScript target version. It can be es5, es6, es2017, etc.

module

Defines the module code generation method, such as commonjs, amd, es2015, etc.

strict

Enables a wide range of type checking behavior that results in a more robust codebase.

{
  "compilerOptions": {
    "target": "es2017",
    "module": "es2015",
    "strict": true
  }
}

Common Use-Cases

Working with React

When you're using TypeScript with React, you'll need to set the jsx compiler option to react.

Using TypeScript with Node.js

For Node.js projects, you'll often use commonjs as your module system. You may also want to set the resolveJsonModule to true to import JSON files.

Frequently Asked Questions

Is tsconfig.json mandatory for a TypeScript project?

No, but without it, you'll lack the control to fine-tune your compiler's behavior, making it very difficult to manage large projects.

Can I have multiple tsconfig.json files in a single project?

Yes, you can have multiple tsconfig.json files for different parts of your project, but they must extend a root tsconfig.json file.

What happens if I don't include the include and exclude properties?

If omitted, TypeScript will include all TypeScript files in the folder containing tsconfig.json and its sub-directories, excluding those in node_modules.

Conclusion

The tsconfig.json file is more than just a configuration file; it's the control center of your TypeScript project. From setting your compiler options to specifying which files to include or exclude, it gives you the power to shape your project just the way you want it. So the next time you find yourself in the TypeScript ocean, remember, the tsconfig.json is your captain’s wheel, steering you towards smoother sailing and away from troubled waters.

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, CodeStudio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Live masterclass