Skip to main content
illustration of random abstract shapes

Deep Dive into TypeScript's tsconfig File

Welcome back, fellow coders! Today we’re taking a journey to the control room of TypeScript — the tsconfig.json file. This configuration file is where you, as a developer, can fine-tune the TypeScript compiler to fit your project’s needs.

With a slew of options, the tsconfig.json file can be a bit intimidating for newcomers. But fear not! Today we’re going to unpack some of the most impactful settings you’ll likely use in your TypeScript projects. Buckle up, it’s time to deep dive!

The Basics of tsconfig.json

First things first, the tsconfig.json file is a configuration file that TypeScript uses to compile your TypeScript code to JavaScript. Located in the root directory of your TypeScript project, this file is automatically read and utilized by the TypeScript compiler.

An example of a simple tsconfig.json file might look like this:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs"
  }
}

In this file, the configuration options are grouped under compilerOptions. The target option specifies the ECMAScript target version, and module specifies the module system the compiler should use.

With the basics covered, let’s unpack some of the root-level configurations and their popular compilerOptions.

Root-level Configurations

  1. compilerOptions: This key-value pair contains options that affect how the compiler behaves. We’ll take a deeper look into specific compilerOptions in the next section.

  2. include: This option allows you to specify an array of glob patterns for files that should be included in the compilation. If not specified, TypeScript includes all TypeScript (.ts, .d.ts, .tsx) files in the project, excluding those listed under exclude.

  3. exclude: This option allows you to list files or folders that should not be included in the compilation process.

  4. extends: This property allows a tsconfig.json file to inherit settings from another configuration file. It’s handy when you have common settings that can be shared across different TypeScript projects.

  5. files: This property allows you to list individual files included in the compilation. It’s important to note that files included using include or files are excluded by default unless explicitly listed.

  6. references: This is a part of TypeScript’s Project References feature. You can specify an array of other projects that are referenced by the current project.

Digging into compilerOptions

Now let’s take a closer look at some of the most commonly used compilerOptions.

  1. target: This option sets the ECMAScript target version. Options include “es5”, “es6”/“es2015”, and onward to “es2020”, or even “esnext” for the latest features.

  2. module: Specifies the module system, with options such as “none”, “commonjs”, “amd”, “system”, “umd”, “es6”, “es2015”, or “esnext”.

  3. lib: This option allows you to specify library files to be included in the compilation. Common options include “es5”, “es6”, “dom”, “dom.iterable”, “webworker”, and others.

  4. outDir and rootDir: These options allow you to set the output directory for your compiled JavaScript code and the root directory of your TypeScript source code, respectively.

  5. removeComments: If set to true, the compiler will remove comments from the output JavaScript files.

  6. strict and its related options (noImplicitAny, strictNullChecks, strictFunctionTypes,

strictBindCallApply): These options enable more stringent type-checking, helping catch more errors at compile-time.

  1. esModuleInterop: This option enables a more ECMAScript-compliant module import and export behavior. It is recommended when using Babel along with TypeScript.

  2. allowJs: If this is set to true, TypeScript will also compile JavaScript files in your project.

  3. sourceMap: When this is set to true, the compiler generates corresponding .map files for the JavaScript files, which can be useful for debugging.

  4. declaration: If set to true, TypeScript generates corresponding .d.ts declaration files for your TypeScript files.

  5. resolveJsonModule: When this option is true, TypeScript will allow importing of JSON modules.

  6. isolatedModules: If this is true, each file is treated as a separate module.

  7. noEmitOnError: When this option is true, TypeScript won’t generate output files if any errors were reported.

  8. baseUrl and paths: These options are used to set up module resolution in a way that emulates the module resolution strategy used by bundlers like Webpack.

Whew! That was a whirlwind tour of TypeScript’s tsconfig.json! The range of options available allows you to tune the TypeScript compiler just right for your project. Though it can seem a tad overwhelming initially, once you understand the purpose of these configurations, you’ll find them empowering.

Next time you’re in TypeScript land, don’t hesitate to tweak your tsconfig.json to suit your needs. After all, who doesn’t like to have a finely-tuned compiler doing their bidding?

Remember, folks, TypeScript isn’t a sprint. It’s a marathon. Take your time with it, and before you know it, you’ll be a TypeScript configuration ace. And on that note, it’s time for us to compile and run. Until next time, happy coding!

Stay in touch

Don't miss out on new posts or project updates. Hit me up on X (Twitter) for updates, queries, or some good ol' tech talk.

Follow @zkMake
Zubin Khavarian's Profile Written by Zubin Khavarian