Understanding "use strict" in TypeScript
Tags: webdev, programming, typescript, javascript
Published: 2023-11-06 19:36:14

TypeScript offers many helpful features to make coding cleaner and more reliable. In this article, we'll take a closer look at one such feature called "use strict." This feature is a simple but important part of TypeScript that can improve the quality and reliability of your code. We'll explore how "use strict" helps you follow best practices and maintain the integrity of your software projects. Let's dive into the world of "use strict" in TypeScript!

## **What is "use strict"?**

Before we delve into how TypeScript uses "use strict," let's understand what it is. "use strict" is a directive in JavaScript that changes the way the code is interpreted. When it is applied, JavaScript operates in a more secure and structured mode. It enforces stricter parsing and error handling, making your code less prone to common coding mistakes.

Here's how to enable "use strict" in JavaScript:

[](
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qize1ekpx3wimprffo50.png))


## **"use strict" in TypeScript**
In TypeScript, the use of "use strict" is a bit different from traditional JavaScript. TypeScript always assumes "use strict" by default. This means that the best practices and error prevention provided by "use strict" are already integrated into your TypeScript code without explicitly adding the directive.

Let's take a look at an example. Consider this TypeScript code snippet:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oouznt6gx2kgaoxcedgc.png)


Under the hood, TypeScript transpile this code into JavaScript and ensures that "use strict" is implicitly present:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyy8nwrw73rza3gc3al0.png)

## **Benefits of Implicit "use strict"**

The implicit "use strict" in TypeScript offers several benefits:

**- Enhanced Code Quality**
By enforcing stricter rules and preventing common coding mistakes, "use strict" in TypeScript helps maintain a higher level of code quality. This leads to fewer runtime errors and a more predictable code behavior.

**- Improved Security**
"Use strict" enhances security by preventing certain types of vulnerabilities and exploits. It reduces the risk of accidentally declaring global variables and mitigates potential security threats.

**- Future-Proofing**
With "use strict" implicitly enabled, your TypeScript code is more compatible with evolving JavaScript standards. You're less likely to encounter issues when adopting new language features and adhering to best practices.

## **When to Use "use strict" Explicitly**
In most cases, TypeScript's implicit "use strict" is sufficient to ensure code quality and security. However, there may be scenarios where you want to use it explicitly. This is relevant when you're working with existing JavaScript code and want to ensure that the "use strict" directive is in place.

To use "use strict" explicitly in TypeScript, you can do the following:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ewudeyutds9rzyl4pd3.png)

By adding the directive at the top of your TypeScript file, you make it clear that you intend to enforce strict mode. It's also useful when integrating TypeScript code into projects with existing JavaScript codebases.

## **Conclusion**
"Use strict" is a valuable feature that enhances the quality, security, and compatibility of your code. In TypeScript, it's always enabled by default, making your development experience more reliable. While you might need to use it explicitly in specific scenarios, TypeScript's implicit "use strict" is your steadfast guardian, ensuring your code remains robust and future-proof.