ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Follow publication

Unlocking the Power of TypeScript’s Compiler Configuration

Irene Smolchenko
ITNEXT
Published in
8 min readJun 7, 2023

--

Colorful configuration wheels spinning in harmony, representing the diverse and interconnected aspects of TypeScript’s compiler configuration
Photo by Tim Mossholder on Unsplash

tsconfig and Compiler Options

Include, Exclude and Files properties

{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},

// all .ts files under the src and tests directories are included in the compilation process
"include": [
"src/**/*.ts",
"tests/**/*.ts",
"styles/*.{css,scss}" // Matches all .css and .scss files in the styles directory
],

// node_modules and dist directories are excluded from the compilation process
"exclude": [
"node_modules",
"dist"
]
}
{
"compilerOptions": {
"outFile": "bundle.js"
},

// only main.ts and utils.ts files in the src directory will be included in the compilation process
"files": [
"src/main.ts",
"src/utils.ts"
]
}

Configuration Inheritance with Extends

// baseConfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist"
}
}
// your tsconfig.json inherits the compilerOptions from baseConfig.json,
// while extending it with additional properties specific to the current project
{
"extends": "./baseConfig.json",
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
// baseConfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
}
}
// advancedConfig.json (extends baseConfig.json)
{
"extends": "./baseConfig.json",
"compilerOptions": {
"outDir": "dist"
}
}
// your tsconfig.json (extends advancedConfig.json)
{
"extends": "./advancedConfig.json",
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}

Output Locations, Tweaks and Emitting On Error

{
"compilerOptions": {
"outDir": "dist"
}
}
{
"compilerOptions": {
"outFile": "bundle.js"
}
}
{
"compilerOptions": {
"removeComments": true
}
}
{
"compilerOptions": {
"sourceMap": true
}
}
{
"compilerOptions": {
"sourceMap": true,
"inlineSourceMap": true
}
}
{
"compilerOptions": {
"sourceMap": true,
"sourceRoot": "./src",
"mapRoot": "./maps"
}
}
// TS will not emit any JS files
{
"compilerOptions": {
"noEmitOnError": true
}
}
// TS will not emit any JS files
{
"compilerOptions": {
"noEmit": true
}
}

Understanding “lib” and ES libraries

{
"compilerOptions": {
"target": "es2018",
"lib": ["ES2018", "DOM", "./typings/myLibrary.d.ts"]
}
}

Strict Flags for Enhanced Safety

Transpiling and Experimental Features

Conclusion

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Irene Smolchenko

🍴🛌🏻 👩🏻‍💻 🔁 Front End Web Developer | Troubleshooter | In-depth Tech Writer | 🦉📚 Duolingo Streak Master | ⚛️ React | 🗣️🤝 Interviews Prep

No responses yet

Write a response