Open In App

What is the purpose of using tsconfig.json file ?

Last Updated : 16 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

tsconfig.json file is a file of JSON format which allows us to point the root level files and different compiler options to setup that require to compile a TypeScript based projects. The existence of this file in a project specifies that the given directory is the TypeScript project folder root. 

The tsconfig.json file mainly consists of the information for the following:

  • CompilerOptions
  • CompileOnSave
  • Files
  • Include
  • Exclude

Let’s take an example file of tsconfig.json file and understand the purpose of each of its parts.

tsconfig.json




{
   "compileOnSave": true,
   "compilerOptions": {
        "module": "system",
        "noImplicitAny": true,
        "removeComments": true,
        "allowUnreachableCode": false,
        "strictNullChecks": true,
        "outFile": "../JS/TypeScript/HelloWorld.js",
        "sourceMap": true
   },
    "files": [
        "program.ts",
        "sys.ts"
    ],
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "src/**/*.spec.ts"
    ]
}


CompilerOptions – It is a property that allows specifying additional options to the TypeScript compiler. Given a list of a few optional settings section of the compilerOptions property that is needed most of the time: 

  • listFiles
  • module
  • outDir
  • outFile
  • rootDir
  • sourceRoot
  • allowUnreachableCode
  • allowJs
  • noImplicitUseStrict
  • strictNullChecks

{
   “compilerOptions”: {
       “module”: “system”,
       “noImplicitAny”: true,
       “removeComments”: true,
       “allowUnreachableCode”: false,
       “strictNullChecks”: true,
       “outFile”: “../JS/TypeScript/HelloWorld.js”,
       “sourceMap”: true
   }
}

CompileOnSave – It is a property that can be used to instruct the IDE to automatically compile the given TypeScript files and generate the output for the same.

{
  “compileOnSave”: true,
  “compilerOptions”: {
       “module”: “system”,
       “noImplicitAny”: true,
       “removeComments”: true,
       “allowUnreachableCode”: false,
       “strictNullChecks”: true,
       “outFile”: “../JS/TypeScript/HelloWorld.js”,
       “sourceMap”: true
  }
}

Files – It is a property that gives a list of TypeScript files that will be included by the compiler. The URL of the files can be both relative or absolute.

“files”: [
       “program.ts”,
       “sys.ts”
   ]

Include – It is a property that allows you to include a list of TypeScript files using the glob wildcards pattern.

“include”: [
       “src/**/*”
   ]

Exclude – It is a property that allows you to exclude a list of TypeScript files using the glob wildcards pattern.

“exclude”: [
       “node_modules”,
       “src/**/*.spec.ts”
   ]



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads