Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript alwaysStrict option

Reference for the alwaysStrict compiler option: parsing every file in strict mode, emitting use strict, what changes at runtime and why modules are strict anyway.

alwaysStrict makes the compiler parse every file as ECMAScript strict mode code and write a "use strict" directive at the top of each emitted JavaScript file. Strict mode removes a handful of old, error-prone JavaScript behaviors: silent failures turn into thrown errors, undeclared variables are no longer created by accident, and this in a plain function call is undefined instead of the global object.

It is part of the strict family. In practice it matters only for scripts and CommonJS output: ES modules (any file with import or export emitted as ESM) are always strict, so for modern code the option mostly documents your intent.

JSON
{
  "compilerOptions": {
    "alwaysStrict": true
  }
}

Try it

TypeScript Silent failures become errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript this in a plain call

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "alwaysStrict": true } }
Part Description
Value true or false. Default: false, but true when strict is enabled.
Effect on parsing Strict-only syntax errors apply: octal literals like 010, with statements, duplicate parameter names, delete of a plain variable and reserved words such as let as names.
Effect on emit "use strict"; is added at the top of each emitted file (not for ESM output, which is strict by definition).

Usage notes

  • Modules are already strict. If your module setting produces ES modules, the directive is redundant and the compiler does not add it. CommonJS output gets the directive.
  • Behavior differences. Assigning to a read-only property, adding a property to a frozen object, or writing to an undeclared name throws TypeError or ReferenceError instead of doing nothing or making a global.
  • Types already catch most of it. The frozen-object write above is also a compile error when typed honestly; the runtime check is a second line of defense for any and casts.
  • Existing directive. A file that already starts with "use strict" is left alone.
  • Declaration files and .d.ts output are not affected; there is no code in them.
  • Turning it off is rarely sensible. Only very old scripts that rely on sloppy-mode this or arguments.callee need it.