Typescript Foundations / Introduction to Typescript

tsconfig.json: Configuration options

πŸ‘‰ tsconfig.json is a config file that tells the TypeScript compiler (tsc):

  • Which files to compile

  • How to compile them (rules, target JS version, strictness, etc.)

When you run tsc (without specifying a file), it reads tsconfig.json to guide compilation.


πŸš€ How to create one

tsc --init

πŸ‘‰ This generates a default tsconfig.json.


βš™οΈ Basic structure

{
  "compilerOptions": {
    // Compiler settings go here
  },
  "include": [
    // Files or folders to compile
  ],
  "exclude": [
    // Files or folders to ignore
  ]
}

🌟 Key compilerOptions to know

Option

What it does

Example

target

JS version to output

"es6"

module

Module system to use

"commonjs", "esnext"

outDir

Where to put compiled JS

"./dist"

rootDir

Where source TS files are

"./src"

strict

Enables all strict type checks

true

esModuleInterop

Allow default imports from CommonJS modules

true

sourceMap

Generate .map files for debugging

true

declaration

Output .d.ts type declaration files

true

noEmit

Don’t output JS, just check types

true

removeComments

Remove comments from JS output

true

allowJs

Allow JS files in the compilation

true

checkJs

Type-check JS files

true

lib

Include library files (e.g., DOM, ES2020)

[ "es2020", "dom" ]


πŸ“ Example tsconfig.json

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

πŸ‘‰ This config:

  • Compiles TypeScript from src/

  • Outputs JS to dist/

  • Uses ES6 syntax + CommonJS modules

  • Enables strict type checking

  • Generates source maps for debugging

  • Ignores test files and node_modules


⚑ Commonly used strict options

If you want to customize strictness:

{
  "strictNullChecks": true,
  "noImplicitAny": true,
  "strictFunctionTypes": true,
  "strictBindCallApply": true,
  "alwaysStrict": true
}

βœ… Tip: Setting "strict": true automatically enables these.


πŸ”₯ Special options

Option

Use case

incremental

Speeds up recompilation in large projects

composite

Required for projects using project references

noEmitOnError

Prevent JS output if type errors

resolveJsonModule

Allow importing .json files

isolatedModules

Ensures compatibility with Babel or other transpilers


πŸš€ How to use

πŸ‘‰ Once tsconfig.json is set:

tsc

βœ… TypeScript will read this config and compile accordingly.


πŸ’‘ Summary

βœ… tsconfig.json gives you full control over TypeScript compilation.

βœ… Helps make builds consistent across teams.

βœ… Can be customized for Node, React, libraries, etc.