~/
hackweb.dev
TypeScript Config & Strict Mode
Quiz
⌘K
...
~/
/tutorials
/ts/ts-config/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
Comment
0 / 300
Typo
Grammar
Broken link
Clarify
Code
en/tutorials/ts/12ts-config
Write
Preview
Diff
# TypeScript Config & Strict Mode The `tsconfig.json` file controls how TypeScript compiles and type-checks your project. ## Basic Structure ```json { "compilerOptions": { "target": "ES2020", "module": "ESNext", "strict": true, "outDir": "./dist", "rootDir": "./src" }, "include": ["src/**/*"], "exclude": ["node_modules"] } ``` **Remember:** Without `tsconfig.json`, TypeScript uses default options that may not suit your project. ## Strict Mode `strict` enables all strict type-checking options: - `strictNullChecks` — Prevents assigning null/undefined to non-nullable types - `noImplicitAny` — Flags implicit `any` types - `strictFunctionTypes` — Strict function parameter checking - `strictPropertyInitialization` — Ensures class properties are initialized **Always enable strict mode** — it catches the most common TypeScript bugs. ## Essential Options ```json { "compilerOptions": { "target": "ES2020", "strict": true, "noEmit": true, "declaration": true, "sourceMap": true, "moduleResolution": "node", "esModuleInterop": true, "resolveJsonModule": true } } ``` - `noEmit` — Type check only, no output (great for CI/CD) - `declaration` — Generate `.d.ts` files for libraries - `sourceMap` — Enable debugging ## Including and Excluding Files ```json { "include": ["src/**/*"], "exclude": ["node_modules", "dist", "**/*.test.ts"], "files": ["src/main.ts"] } ``` **Tip:** `include` uses glob patterns; `files` lists specific files. ## Project References Split large projects into smaller, manageable units: ```json { "extends": "./tsconfig.base.json", "compilerOptions": { "outDir": "./dist", "rootDir": "./src" }, "references": [{ "path": "./tsconfig.lib.json" }] } ``` **Remember:** Project references enable faster builds and better separation of concerns. ## Best Practices - Always enable strict mode - Use source maps in development for debugging - Generate declarations for libraries with `declaration: true` - Use project references for large codebases - Enable `noUnusedLocals` and `noUnusedParameters` to clean up dead code ## Common Mistakes - Not enabling strict mode misses many type errors - Using a target that's too old forces unnecessary transpilation - Not setting `rootDir` can cause confusing output paths - Not excluding test files can cause type conflicts
No changes yet
Reset to original
Submit suggestion
cancel