CodecPath

Guide

TypeScript Strict Mode vs JavaScript Strict Mode

TypeScript strict mode wins for catching application bugs before execution; JavaScript strict mode changes runtime rules but does not check types.

CodecPath editors4 min read
JavaScript strictmodeChanges runtimesemanticsTypeScript strictmodeFinds type errors beforeexecutionRuntime validationChecks external datawhile running
Three distinct safety layers: TypeScript catches the broadest set of source defects, while explicit validation handles data arriving after compilation.

Which strict mode catches more bugs?

TypeScript wins. It checks program types before execution, catching mismatched arguments, unsafe nullable values, bad property access, and other defects that JavaScript's runtime restrictions cannot identify.

The names mislead people. These are separate safety systems operating at different times.

JavaScript strict mode modifies how the engine interprets and executes certain code. It catches a smaller class of mistakes, often only after the faulty statement runs.

TypeScript's strict setting is an umbrella switch. The TypeScript compiler reference says it enables a family of stronger type-checking options, and that family may grow as the compiler changes.

That version detail matters. Upgrading TypeScript can reveal fresh errors without anyone editing tsconfig.json, because a newer compiler may place another check beneath the existing umbrella.

The decision is still easy. Use TypeScript strict mode as the baseline for a TypeScript application, then add runtime validation at every boundary where untrusted data enters.

What does JavaScript strict mode change?

It changes runtime semantics. A "use strict" directive makes several silent mistakes throw errors, prohibits ambiguous legacy syntax, and alters details such as this inside an unbound function call.

Consider an accidental global. In an old-style sloppy script, assigning to an undeclared name can create a property on the global object; under strict execution, that assignment throws ReferenceError when the engine reaches it.

That timing is crucial. An unexecuted branch produces no runtime error.

Strict execution also prohibits with, rejects duplicate parameter names in ordinary functions, and makes assignment to a non-writable property fail loudly. MDN documents the complete list in its JavaScript strict mode reference.

Modules need no directive. ECMAScript modules and JavaScript class bodies are strict automatically, so putting "use strict" at the top of an .mjs file changes nothing.

Its scope remains narrow. JavaScript strict mode provides behavior tweaks to the runtime of the JavaScript engine, but it cannot know that account.owner may be undefined, that a callback has the wrong signature, or that a function promised to return a number.

What does TypeScript strict mode actually enable?

It enables multiple checks. Setting "strict": true activates related compiler options covering nullability, implicit any types, function compatibility, class-property initialization, catch variables, and the meaning of this.

Start here:

JSON
{
  "compilerOptions": {
    "strict": true
  }
}

strictNullChecks usually causes the most useful disruption. With it enabled, null and undefined stop flowing freely into unrelated types, which forces code to prove that a value exists before reading its properties or passing it onward.

Other members include noImplicitAny, strictFunctionTypes, strictPropertyInitialization, useUnknownInCatchVariables, and noImplicitThis. The TSConfig option index lists the flags available in the current compiler.

Compilation is the limit. The types vanish from emitted JavaScript.

Consequently, an interface does not inspect an API response, database row, environment variable, local-storage entry, or the result of JSON.parse, regardless of how carefully that interface describes the expected object.

Assertions bypass protection too. Writing payload as User instructs the checker to accept a claim; it inserts no runtime test and cannot repair missing fields.

That is the trap. A supposedly strict codebase can still fail in production when external bytes cross into trusted application code without validation.

When should a project enable both?

Enable both by default. TypeScript should check source code during compilation, while JavaScript strict semantics should govern the emitted program unless its module format already guarantees them.

For ES modules, the runtime half is automatic. Add "strict": true to tsconfig.json for the static half.

CommonJS output is murkier because TypeScript commonly emits "use strict", yet bundlers, minifiers, wrappers, and concatenation steps can alter the final execution context, making the built artifact more authoritative than the source file.

Inspect that artifact. Do not infer its semantics from an editor badge.

The loser has one clear win. Choose JavaScript strict mode alone for a small plain-JavaScript script that must avoid a compiler and build pipeline; accidental globals and silent writes are worth catching even without static types.

For a substantial TypeScript repository, go further. Configuring TypeScript compiler options beyond strict mode with noUncheckedIndexedAccess, exactOptionalPropertyTypes, and noImplicitOverride closes useful gaps left outside the umbrella.

Then guard the boundary. Schema validation or explicit runtime checks must verify network responses, parsed files, message-queue payloads, and user input because neither strict system proves that external data has the promised shape.

FAQ

Does TypeScript add "use strict" to generated JavaScript?
Sometimes. CommonJS output commonly contains the directive, while ES modules are strict by definition; compiler settings and later build steps can affect the delivered artifact, so inspect that file when runtime semantics matter.
Does strict mode make TypeScript runtime-safe?
No. Type information is erased during compilation, leaving API responses, form fields, storage values, and parsed JSON unchecked unless the running program validates them.
Can strict checks be adopted gradually?
Yes. Enable individual flags, repair errors package by package, and switch on "strict": true once the remaining failures are manageable; replacing every complaint with any merely silences the checker and preserves the underlying risk.
Is strict true enough for a new TypeScript project?
It is the baseline. New projects should also evaluate noUncheckedIndexedAccess, exactOptionalPropertyTypes, and noImplicitOverride, since those valuable checks remain outside the strict umbrella.

Related guides