Strict Mode & tsconfig
strict flag, strictNullChecks, noImplicitAny, noUncheckedIndexedAccess, and essential tsconfig options
Skills taught: ts.tsconfig, ts.strict-flag, ts.strictNullChecks, ts.noImplicitAny, ts.noUncheckedIndexedAccess
tsconfig.json essentials
{
"compilerOptions": {
// — Target & Output —
"target": "ES2022", // which JS version to emit
"module": "ESNext", // module format (ESNext for bundlers)
"outDir": "./dist", // where compiled JS goes
"rootDir": "./src", // source root
"declaration": true, // emit .d.ts for library authors
"sourceMap": true, // emit source maps for debugging
// — Strict mode (ALWAYS ON for new projects) —
"strict": true, // enables all strict checks below
// "strictNullChecks": true, // null/undefined not assignable to other types
// "noImplicitAny": true, // error on implicit any
// "strictFunctionTypes": true,
// "strictPropertyInitialization": true,
// — Extra recommended flags —
"noUncheckedIndexedAccess": true, // arr[i] is T | undefined
"noImplicitReturns": true, // all paths must return
"noFallthroughCasesInSwitch": true,
"exactOptionalPropertyTypes": true,
// — Module resolution —
"moduleResolution": "bundler", // for Vite/esbuild/webpack 5
"esModuleInterop": true, // allow default import from CJS
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true, // import JSON files
"skipLibCheck": true, // skip checking .d.ts files
// — JSX (for React projects) —
// "jsx": "react-jsx"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
What strict mode catches
// strictNullChecks — null must be handled
function getLength(s: string | null): number {
// Without strictNullChecks: s.length would compile (crash at runtime)
// With strictNullChecks:
if (s === null) return 0;
return s.length; // TypeScript knows: s is string here
}
console.log(getLength('hello')); // 5
console.log(getLength(null)); // 0
// noImplicitAny — must annotate when TypeScript can't infer
// Without noImplicitAny: TypeScript silently uses 'any' here:
// function log(msg) { ... } ← msg is any — no checking!
// With noImplicitAny: Error — Parameter 'msg' implicitly has an 'any' type
function log(msg: string): void { // must annotate
console.log('[LOG]', msg);
}
// noUncheckedIndexedAccess — array access returns T | undefined
const items = ['a', 'b', 'c'];
const first = items[0]; // type: string | undefined (not just string!)
if (first !== undefined) {
console.log(first.toUpperCase()); // safe
}
// strictPropertyInitialization — must init class props
class User {
name: string; // Error without assignment: not initialized
constructor(name: string) {
this.name = name; // OK — assigned in constructor
}
}
console.log(new User('Alice').name);
Frequently Asked Questions
My existing project has many strict mode errors. How do I enable strict incrementally?
Instead of strict: true, enable individual flags one at a time: start with noImplicitAny (highest value), then strictNullChecks (most impactful), then the others. Fix errors for each flag before enabling the next. Alternatively, use // @ts-check in JS files or ts-migrate tool to do the migration semi-automatically.