alwaysStrict

Parses every file in ECMAScript strict mode and emits "use strict" at the top of the output.

Signature
"alwaysStrict": boolean  // Default: true if strict; false otherwise
  • Added in TypeScript 2.1

Parameters

ParameterTypeDescription
$alwaysStrictoptionalboolean= true if strict; false otherwiseWhen true, source files are parsed in ECMAScript strict mode and a "use strict" prologue is emitted for each output file.

Errors

Nothing at runtime from the flag itself; strict-mode parsing rejects constructs such as duplicate parameter names, legacy octal literals and with statements at compile time. At runtime, strict mode makes previously silent failures throw TypeError.

Changelog

  • PHP 5.0noImplicitUseStrict, the flag that suppressed the emitted prologue, was deprecated.
  • PHP 5.5noImplicitUseStrict no longer has any effect, so the prologue can no longer be suppressed that way.

alwaysStrict does two things: it parses your source as ECMAScript strict mode, and it writes "use strict"; at the top of each emitted file. Strict mode was introduced in ES5; it turns a set of silent failures into thrown errors and removes constructs that make the language hard to optimise.

What strict mode changes at runtime

  • this is undefined in a plain function call instead of the global object
  • Assigning to a frozen, non-writable or non-existent property throws TypeError instead of failing silently
  • delete on a non-configurable property throws
  • with, legacy octal literals like 010, and duplicate parameter names are syntax errors

What trips people up

The "use strict" prologue is not emitted for ES modules, because modules are always strict by specification — so a project with module set to an ESM format sees no visible change in output and people assume the flag is broken. It is not: the parsing half still applies. Conversely, in CommonJS output the prologue appears in every file and shows up in diffs.

Example

Example · typescript
// "alwaysStrict": true -> each emitted file starts with "use strict";

const settings = Object.freeze({ retries: 3 });

try {
  // Silent no-op in sloppy mode; TypeError in strict mode.
  (settings as { retries: number }).retries = 99;
  console.log("assignment silently ignored");
} catch (e) {
  console.log("threw:", (e as TypeError).constructor.name);
}

console.log(settings.retries); // 3
// strict output -> threw: TypeError
// sloppy output -> assignment silently ignored

When to use it

  • Guaranteeing emitted CommonJS files run in strict mode regardless of how they are loaded
  • Catching legacy octal literals or duplicate parameter names when porting old JavaScript
  • Making silent writes to frozen configuration objects throw instead of vanishing

Frequently asked questions

Why is "use strict" not in my compiled output?

Because your output is ES modules, which the specification already runs in strict mode, so TypeScript omits the redundant prologue. alwaysStrict still parses your source in strict mode; only the emitted directive is skipped.

What is the difference between alwaysStrict and strict?

alwaysStrict is one member of the family that strict turns on, and it is the only one that changes emitted JavaScript rather than just type checking. strict additionally enables strictNullChecks, noImplicitAny and the rest.

How do I stop TypeScript emitting "use strict"?

Set "alwaysStrict": false (and do not enable strict, or override it explicitly). The old noImplicitUseStrict flag was deprecated in 5.0 and stopped having any effect in 5.5.

More examples

this in a plain function call

The first log distinguishes the two modes; the second is the same either way because a method call supplies a receiver. Note the explicit this parameter, which noImplicitThis would otherwise demand.

Example · typescript
// Under "use strict", an unbound function call has `this === undefined`.
// Under sloppy mode it is the global object.
function reportThis(this: unknown) {
  return this === undefined ? "undefined" : "globalThis";
}

console.log(reportThis()); // strict: undefined / sloppy: globalThis

const obj = { name: "box", get: reportThis };
console.log(obj.get()); // globalThis (a method call always has a receiver)

What strict-mode parsing rejects outright

Prints 3, 8, 3.14. The rewritten forms are what the strict parser accepts; the commented originals never reach the emitter, so this half of the flag is a compile-time gate rather than a runtime one.

Example · typescript
// All three are syntax errors when the file is parsed in strict mode:
//
//   function dup(a, a) { return a; }   // duplicate parameter name
//   const legacyOctal = 010;           // use 0o10 instead
//   with (Math) { console.log(PI); }   // 'with' is not allowed

function dup(a: number, b: number) {
  return a + b;
}

console.log(dup(1, 2));   // 3
console.log(0o10);        // 8
console.log(Math.PI.toFixed(2)); // 3.14

Discussion

  • Be the first to comment on this lesson.