Configuring tsconfig.json

Eman Awad
Nov 8
2 min read
post_comment0 Comments
post_like1 Likes

#What is a tsconfig.json ?

tsconfig.json file specifies TS compiler settings required to compile TS files in your project. Using it we can customize these settings based on your project requirements such as target version, module system, and strictness. and other options.

To create tsconfig.json invoke the following command :

tsc --init

After invoking the previous command we will note that tsconfig.json is created in our project directory.

image

Now we will get to know some compiler settings that we can customize through tsconfig.json file:

  • target : specifies the ECMAScript version of the TS compiler (e.g., "es5", "es6"). It determines the version of JavaScript that the TypeScript code will be compiled into.

  • module : defines the module system to be used (e.g., "commonjs", "es6").

  • strict : enforces strict type checking rules.

  • outDir : specifies output directory for the compiled JS files.

  • rootDir : specifies the root directory for TS source files.

  • allowJs : allows the use of JS files in your TS projects.

  • files : specifies an allowlist of files to be to be compiled.

  • include : Lists file or directory patterns that should be included in compilation.

  • exclude : Lists file or directory patterns that should be excluded from compilation.

  • extends : Allows you to extend or inherit configurations from another tsconfig.json file.

#Here's an example of a tsconfig.json file:

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

In this tsconfig.json file we defined that TS compiler should compile TS code to “es6” , use “commonjs” module, enforce strict type checking rules, use “./dist” as our output directory to store the compiled JS files and use “./src” as source directory for TS files. It also includes TS files under “src” directory and excludes any TS files under “node_modules” directory.

You can learn more about the TS configuration options here .

📍References :

🔗 TypeScriptLang | GCORE

You are not logged in.