A Strict tsconfig.json for Modern TypeScript Projects
tsconfig.json has over a hundred options. These are the ones that matter most for catching bugs in a modern project.
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noFallthroughCasesInSwitch": true,
"exactOptionalPropertyTypes": true,
"verbatimModuleSyntax": true,
"isolatedModules": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"outDir": "dist",
"sourceMap": true
},
"include": ["src"]
}
The important ones
strict turns on a family of checks, including strictNullChecks (no more "undefined is not a function") and noImplicitAny.
noUncheckedIndexedAccess makes array and record lookups possibly undefined, which matches reality:
const names = ['Asha'];
const second = names[1]; // string | undefined with this flag
second.toUpperCase(); // Error: 'second' is possibly 'undefined'
exactOptionalPropertyTypes distinguishes "property missing" from "property set to undefined", which matters for PATCH-style updates.
verbatimModuleSyntax and isolatedModules keep your code compatible with fast single-file transpilers such as esbuild, SWC and Node's type stripping.
Module settings
- Node.js apps and libraries:
"module": "nodenext". - Code bundled by Vite, Next.js or esbuild:
"module": "esnext"with"moduleResolution": "bundler".
Adopting strictness in an existing codebase
- Turn on
strictand count the errors. - Fix one folder at a time, or use
// @ts-expect-errorwith a TODO on the hardest cases. - Add the extra flags one by one once
strictis clean.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.