Exploring TypeScript Compiler Options

Tutorial 5 of 5

1. Introduction

In this tutorial, we will explore various TypeScript compiler options and learn how to set them in the tsconfig.json file. These options control the behavior of the TypeScript compiler and can greatly affect the output of your code. By the end of this tutorial, you will have a clear understanding of the most commonly used compiler options and how each one can affect your TypeScript projects.

Prerequisite: Basic knowledge of TypeScript is required.

2. Step-by-Step Guide

The tsconfig.json file in a TypeScript project is where we specify the root files and the compiler options. It's a JSON file that contains a compilerOptions property which we can use to set various options.

Let's start exploring some of the most common compiler options:

2.1 target

The target option specifies the ECMAScript target version. The TypeScript compiler will output code that conforms to this version. For example:

{
  "compilerOptions": {
    "target": "es5"
  }
}

In this case, the TypeScript compiler will output ES5 compatible code.

2.2 module

The module option specifies the module code generation. Common options are none, commonjs, amd, system, umd, es6, es2015, or ESNext.

{
  "compilerOptions": {
    "module": "commonjs"
  }
}

2.3 strict

This option enables a wide range of type checking behavior that results in more robust programs. The strict option is a boolean and is set to false by default.

{
  "compilerOptions": {
    "strict": true
  }
}

3. Code Examples

Let's consider a few practical examples:

3.1 Example: Using the noImplicitAny option

The noImplicitAny flag ensures that you do not use any types in your code that the compiler infers as any.

{
  "compilerOptions": {
    "noImplicitAny": true
  }
}

When you use this option and try to compile a TypeScript file with a variable of any type inferred by the compiler, it will throw an error.

3.2 Example: Using the outDir and rootDir options

The outDir option is used to redirect output structure to the directory.

The rootDir option is used to specify the root directory of input files.

{
  "compilerOptions": {
    "outDir": "./built",
    "rootDir": "./src"
  }
}

The TypeScript compiler will take the TypeScript files from the src directory, compile them into JavaScript, and then put the resulting files in the built directory.

4. Summary

In this tutorial, we have explored various TypeScript compiler options and learned how to set them in the tsconfig.json file. Understanding these options is crucial for working with TypeScript projects.

Next, you can further explore the official TypeScript documentation to learn about other compiler options.

5. Practice Exercises

  1. Create a TypeScript project and play around with different target options. Observe how the compiled JavaScript code changes.
  2. Enable the strict option in your TypeScript project. Try to compile the project and fix any errors that come up.
  3. Use the outDir and rootDir options to specify the input and output directories for your project.

Solutions and explanations:

  1. The compiled JavaScript code will be different for different target options because it will conform to the ECMAScript version specified.
  2. When the strict option is enabled, you will probably encounter some errors because this option enables stricter type checking. You will need to fix these errors by adding the necessary type annotations.
  3. The outDir and rootDir options just change the input and output directories for your TypeScript project. So, you just need to confirm that the TypeScript compiler is taking the input files from the correct directory and outputting the compiled JavaScript files to the correct directory.