Courses / TypeScript
1. tsconfig.json (basic)
What tsconfig.json is and how to create a minimal configuration for gradual TypeScript adoption.
How It Works
Think of tsconfig.json as a project's "compiler settings sheet": it names the input files (or directories) and the rules the compiler should follow when checking or emitting code.
tsc reads tsconfig.json from the current directory to decide which files to process.
"include" and "exclude" control which files are checked; files not matched are ignored.
"compilerOptions" toggles behaviors like allowing JS, target output, and module resolution.
See It
The code shows a minimal tsconfig.json object: allowJs lets JS files be part of the project, checkJs asks TypeScript to type-check JavaScript files, noEmit prevents writing compiled output (useful for checks only), and include selects files under src/. Try changing "checkJs" to false to see TypeScript stop reporting JS type errors.
/*
Save as minimal tsconfig.json (this is valid JSON, shown in a JS string for runnable demo)
This demo prints the JSON so you can see it; in a real project save the JSON to tsconfig.json
*/
const minimal = {
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"target": "ES2018"
},
"include": ["src/**/*"]
};
console.log(JSON.stringify(minimal, null, 2));
/* To use: save JSON to tsconfig.json and run `npx tsc --project .` */Use It
Adding type checking incrementally to a large Node.js codebase where you don't want to convert all files at once.
// Example: project structure has some JS and some .ts files under src/
// Minimal tsconfig.json you would create:
const tsconfig = {
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"module": "commonjs",
"target": "ES2019"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
};
console.log("Write this to tsconfig.json and run `npx tsc --project .` to check src/");This allows TypeScript to surface type problems in existing JavaScript without forcing an immediate migration to .ts or emitting compiled files.
Break It
What will the TypeScript compiler actually check when you run tsc with this config?
// Broken misconception: trying to limit checks by listing a single file in "files"
// but still expecting checkJs to run on other JavaScript in the project
const tsconfig = {
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true
},
"files": ["src/specificFile.js"]
};
console.log(JSON.stringify(tsconfig, null, 2));
/* Prediction: TypeScript will check all JS under src/ because checkJs is true. */When "files" is present, TypeScript only includes the explicitly listed files in the program; "checkJs" doesn't expand that list — it only tells the compiler to type-check any JavaScript files that are included. "noEmit" only controls output, not whether files are part of the program.
Fix it by replacing "files" with an "include" pattern like "src/**/*" so all JS under src is part of the project.
const tsconfig = {
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true
},
"include": ["src/**/*"]
};
console.log(JSON.stringify(tsconfig, null, 2));
/* Now running `npx tsc --project .` will check all JS under src/ */Level Up
incremental "allowJs -> migrate to .ts": Start with allowJs and checkJs, then gradually rename files to .ts and enable stricter options to migrate safely.
composite projects (project references): Use tsconfig "references" to split a large repo into multiple typed subprojects for faster builds and clearer boundaries.
strict mode adoption: After files are included, progressively enable stricter compilerOptions (strict, noImplicitAny) to tighten type safety.
Prove It
Create a tsconfig.json object that makes the compiler check JavaScript files under "lib/", ignore "lib/vendor/**", and not emit output.
JavaScript / TypeScript
tsconfig.json (basic)
A tsconfig.json file tells the TypeScript compiler which files to check and which options to use when compiling.
"I want to add TypeScript checks to parts of my JavaScript project without rewriting everything at once."
tsconfig.json is a JSON file that configures the TypeScript compiler (tsc): which files to include/exclude and compiler options; a minimal file can enable gradual adoption by only turning on basic checks and allowing JS files.
"I want to add TypeScript checks to parts of my JavaScript project without rewriting everything at once."
tsconfig.json is a JSON file that configures the TypeScript compiler (tsc): which files to include/exclude and compiler options; a minimal file can enable gradual adoption by only turning on basic checks and allowing JS files.