alwaysStrict: ES5 Strict Mode Emission
What you'll learn: How the alwaysStrict compiler option ensures all emitted JavaScript files use strict mode for safer runtime behavior.
What This Option Does
The alwaysStrict option tells TypeScript to:
- Parse all TypeScript files as if they were in strict mode
- Emit
"use strict";at the top of every generated JavaScript file
This guarantees your compiled code runs with JavaScript's strict mode enabled, which catches common runtime errors and prevents unsafe operations.
Why It Matters
JavaScript's strict mode (introduced in ES5) prevents several problematic behaviors:
- Using undeclared variables throws errors instead of creating globals
- Assigning to read-only properties fails loudly
- Duplicate parameter names are forbidden
- The
thisvalue in functions isundefinedinstead of the global object
Think of it like this: If TypeScript is your compile-time safety net, strict mode is your runtime safety net. The alwaysStrict option ensures that second net is always deployed.
When It's Enabled
The alwaysStrict option is automatically enabled when you turn on the strict flag. You can also enable it individually:
{
"compilerOptions": {
"alwaysStrict": true
}
}
What Gets Emitted
With alwaysStrict: true, TypeScript adds "use strict"; to the top of every output file, ensuring strict mode enforcement at runtime regardless of your module system.
Key Takeaway: alwaysStrict ensures your compiled JavaScript runs in strict mode, catching runtime errors that TypeScript can't detect at compile time—it's the final layer of safety for your code.