tsconfig & Strict Mode
The file that configures the TypeScript compiler, and the one setting that turns on its strongest safety checks.
What is it?
Which files to check, which JavaScript version to output, how strict to be: all of this lives in tsconfig.json at the root of a TypeScript project. It tells the compiler (and your editor) which files belong to the project, where compiled output goes, and dozens of options for how picky the type checker is.
One option stands out: strict. "strict": true isn't one check; it turns on a bundle of stricter settings at once. For example, every variable's type must be known instead of silently becoming any, and you must handle values that might be null or undefined.
Most new projects enable it from day one, because adding strictness to a large, loose codebase later is far more painful.
Explain like I'm 10
tsconfig.json is the referee's rulebook: which parts of the field are in play and how strictly fouls are called. strict: true tells the referee "call every foul" instead of only the obvious ones. It catches far more, but play stops more often until everyone follows the rules.
Examples
A minimal tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}target sets the JavaScript version of the output. module sets the output's module system. outDir/rootDir control where compiled files go. include lists the project's files. strict turns on the full bundle of stricter checks.
What strict mode actually catches
// with "strict": false (or strictNullChecks off)
function getLength(text: string) {
return text.length;
}
let input: string = null; // allowed without strict — a silent trap
getLength(input); // crashes at runtime: "Cannot read properties of null"
// with "strict": true
let strictInput: string = null;
// Error: Type 'null' is not assignable to type 'string'.
// You are now forced to handle it explicitly:
let safeInput: string | null = null;
if (safeInput !== null) {
getLength(safeInput); // only reachable once null has been ruled out
}Without strictNullChecks (part of strict mode), null can silently pass as a string and crash at runtime. With strict mode, TypeScript makes you handle null before using the value, at compile time instead of in production.
How it works
When the compiler (or your editor's TypeScript integration) starts, it finds the project's tsconfig.json and reads compilerOptions: which files to include, which JavaScript features to allow or compile away, and which kinds of type errors to report.
"strict": true is shorthand for a list of individual flags, including:
noImplicitAny: errors when the compiler can't infer a type, instead of quietly usingany.strictNullChecks: treatsnullandundefinedas separate types, not assignable to everything.- Several others (see the diagram).
You can still set each flag individually for strictness in some areas but not others, but strict is the usual all-at-once starting point.
tsconfig.json
↓
compilerOptions read by tsc / editor
↓
strict: true expands into:
- noImplicitAny
- strictNullChecks
- strictFunctionTypes
- strictBindCallApply
- strictPropertyInitialization
- noImplicitThis
- alwaysStrict
- useUnknownInCatchVariables
↓
every one of those checks applied while compilingWhy does it exist?
Without a shared config, each developer and editor could apply different rules about what counts as an error, so the project's guarantees would vary by machine. tsconfig.json sets those rules once for the whole project.
strict exists because TypeScript's strictness flags were added gradually, off by default for backward compatibility. Bundling them under one flag gives new projects an easy, well-tested way to get the full intended safety without finding and enabling each flag.
When to use it
Turn on "strict": true for almost every new project. It pays for itself many times over by catching real bugs (like unhandled null values) at compile time. Adjust individual compilerOptions (like target for the environments you support, or paths for import aliases) when a project needs it.
When not to use it
Turning on strict all at once in a large, loosely typed codebase can surface a flood of existing errors. There it's often more practical to enable the individual strict flags one at a time, fixing each category of error before the next.
Common mistakes
Thinking
strict: trueis one check. It's a bundle of flags (noImplicitAny,strictNullChecksand others), each catching a different kind of mistake.Starting a new project with
strictoff "for now." Adding strictness to loosely written code later is far more work than starting strict.Not realizing the editor's TypeScript server may need a restart after
tsconfig.jsonchanges, then wondering why new errors don't appear.
Practice exercises
- Easy:
Write a minimal
tsconfig.jsonfor a new project targeting ES2020, withstrictenabled and source files undersrc/. - Medium:
Write a function that assigns
nullto astring-typed variable. Explain the error you get oncestrictNullChecks(part ofstrict) is on, and how to fix it properly. - Hard:
List three flags that
strict: trueturns on. For each, write a short example of a mistake it catches that non-strict TypeScript allows.
Interview questions
What is `tsconfig.json` for?
It's the config file at a project's root that controls the TypeScript compiler: which files to include, which JavaScript version to output, and which type-checking rules to enforce.
What does `"strict": true` actually do?
It's shorthand that turns on a bundle of stricter compiler flags at once, including noImplicitAny and strictNullChecks. It isn't a single check itself.
What does `strictNullChecks` specifically catch?
It stops null and undefined from being assignable to every other type, so you must handle a possibly missing value before using it.
List the individual compiler flags that `strict: true` bundles together.
noImplicitAny, strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, noImplicitThis, alwaysStrict and useUnknownInCatchVariables. strict turns them all on, and each can also be set individually.
What does `noImplicitAny` specifically do, separate from `strictNullChecks`?
It errors when the compiler can't infer a type (an untyped function parameter, for instance) and would otherwise fall back to any. It makes every value have a known type; how null/undefined are treated is strictNullChecks's job.
What does `strictFunctionTypes` change about how function parameter types are checked?
Parameters are checked contravariantly instead of bivariantly. A function expecting a narrower parameter type can't be used where one expecting a wider type is required, since it could be called with an argument it can't handle. Without it, some unsound substitutions were allowed for backward compatibility.
What does `strictPropertyInitialization` require of class fields, and how does it depend on `strictNullChecks`?
Every class property whose type isn't optional or undefined must be assigned by the end of the constructor (or have an initializer); otherwise it would be undefined at runtime despite its type. It only works with strictNullChecks; without it, undefined is assignable to everything, so there's nothing to flag.
What does `useUnknownInCatchVariables` change about `catch` blocks?
The caught error is typed unknown instead of any. JavaScript can throw any value, not just Error objects, so you must narrow it (e.g. err instanceof Error) before accessing its properties.
What does `alwaysStrict` do, and how is it different from the other strict-family flags?
It's not a type-checking flag. It emits "use strict"; in the output and parses input as strict-mode JS. The other flags govern the type checker; alwaysStrict governs emitted code and parsing, catching sloppy-mode pitfalls like accidental globals.
What does `strictBindCallApply` add on top of the other strict flags?
It checks arguments to .bind(), .call() and .apply() against the original function's parameter types. Without it, those methods were typed loosely and let mismatched arguments through.
A large, several-year-old codebase currently has `strict: false`. What's the recommended migration approach, rather than flipping `strict: true` directly?
Enable the flags one at a time, often noImplicitAny first, then strictNullChecks, fixing each flag's errors before the next. That avoids facing every category of error across the codebase at once.
Are `exactOptionalPropertyTypes` and `noUncheckedIndexedAccess` included when you turn on `strict: true`?
No. Despite the names, both are separate opt-ins and not part of the strict bundle. You must enable each yourself.
What does `noUncheckedIndexedAccess` do, and why isn't it part of the default `strict` bundle?
It adds | undefined to the result of indexing through a string/number index signature, since the key might not exist at runtime. It's left out of strict mainly because it's very noisy on existing code that assumes every key is present.
After enabling `strict: true` on an existing project, dozens of pre-existing null/undefined errors appear across files nobody's touching right now. What's a practical way to manage that without turning strict back off?
Suppress the specific existing errors you're not ready to fix (e.g. a narrow // @ts-expect-error) while new code is still checked. Or migrate flag by flag instead of adopting the whole bundle at once, to keep the errors manageable.
Besides `compilerOptions`, what does the `include`/`exclude`/`files` section of `tsconfig.json` control?
Which files are part of the project. include (often a glob like "src/**/*") lists what to check, exclude removes matches (commonly node_modules and build output), and files lists exact entry files instead of a glob. None of this affects strictness, only which files are checked.
What's the difference between `target` and `lib` in `compilerOptions`?
target sets the JavaScript syntax version the compiler emits. lib sets which built-in type declarations are available (whether Promise, newer Array methods or DOM types like document are known). They're often set together, but changing one doesn't change the other.
A Node.js backend project that never runs in a browser still has DOM types available by default, and code referencing `document` doesn't error as expected. What setting controls this?
lib. By default it's inferred from target and usually includes "dom". Setting lib to something like ["ES2020"] (without "dom") removes the DOM globals, so document or window correctly error.
What does `esModuleInterop` do, and what problem does it fix?
It changes how default imports from CommonJS modules work, so import foo from "some-cjs-package" works even without a real ES-module default export. Without it, you often need the awkward import * as foo from "..." for a module using module.exports = ....
Why does TypeScript centralize configuration in a single `tsconfig.json` rather than letting each file specify its own compiler options?
Type guarantees only mean something if every file follows the same rules. If one file could check more loosely, a value typed string there might not really be a string when it reaches another file. One shared config keeps guarantees consistent for every file and contributor.
What does `noEmitOnError` do, and why might a project want it enabled?
It stops the compiler emitting any JavaScript when there are type errors, instead of emitting anyway. Projects use it so code with type errors can never be built and shipped by accident, making type errors hard build failures.
Two developers run `tsc` on the same code with the same `strict: true` config and get different errors. What's a likely non-strict-related explanation?
Different TypeScript versions. tsconfig.json sets options, not the compiler version, and newer versions add checks or tighten inference. Pin TypeScript as a project dependency instead of relying on a global install.
You added a new strict flag to `tsconfig.json`, but your editor still isn't reporting the new errors it should. What's the likely fix?
Restart the editor's TypeScript language server (or reopen the project). It doesn't always re-read tsconfig.json on save, so new errors may not appear in the editor until a restart, even though a fresh tsc run catches them.