Article Teaser
Author Image

By Dominik, June 22, 2023

The amazing “using” keyword from Typescript 5.2

TypeScript is gearing up for its 5.2 release, packed with proposed features from the TC39 committee. These additions aim to enhance developer productivity and code quality. An exciting and highly anticipated addition is the proposed introduction of the "using" keyword. Currently in Stage 3 of the TC39 proposals, this feature is on track to become available for developers in the near future. Its inclusion promises to bring significant benefits and enhance the language's capabilities.

The new “using” keyword

TypeScript 5.2 will introduce a feature inspired by the C# language. The "using" statement in the C# language ensures that native resources, such as file handles, that are consumed by objects are automatically cleaned up. Types that support this have to implement the
IDisposable
interface. A proposal for explicit resource management in the TC39 may provide this capability to JavaScript, which currently lacks one. The expanding capabilities of JavaScript, particularly its use outside of browsers, have made this field more significant.

What is Symbol?

A Symbol is a unique and immutable data type introduced in ES6. It is often referred to as a "primitive" data type alongside strings, numbers, booleans, null and undefined. Symbols are used to create unique identifiers that can be used as property keys in objects. Symbols are created using the
Symbol()
function, which returns a new and distinct Symbol value each time it is called. For example:

Symbols are primarily useful when you need to create object properties that are guaranteed to be unique, even if they have the same name as other properties. This helps avoid naming collisions and unintended property overwriting. EC6 introduced global symbols, which are predefined and accessible globally, providing a standardized way to access common behaviors or meta-information associated with objects. In TypeScript, the Symbol type represents a unique symbol value. Symbols in TypeScript are similar to symbols in JavaScript, but with the added benefits of static typing. You can use the Symbol type to declare and create unique symbols in your TypeScript code. One significant advantage of TypeScript's Symbol type is static type checking, which enables you to identify potential symbol-related errors during compilation. This ensures consistent and correct usage of symbols throughout your codebase. Symbols are particularly valuable in TypeScript when you need to establish distinct keys for object properties, implement custom behaviors or define specialized methods. They serve as a robust mechanism for creating and manipulating unique identifiers in a type-safe manner.

A new global symbol

A new global symbol called
Symbol.dispose
will be added to JavaScript. When a function is assigned to
Symbol.dispose
, it designates an object as a "Resource" according to the explicit resource management proposal. A "Resource" refers to an object with a specific lifetime. Effective resource management is possible by combining the
using
keyword with the
Symbol.dispose
symbol. This makes it possible for JavaScript code to handle resources in a more controlled manner. Similar to that, we can handle asynchronous actions with
Symbol.asyncDispose
.

Use Cases for Symbol.dispose / Symbol.asyncDispose

One example of use could be preventing data corruption or loss. In scenarios where resources like database connections are used, it's crucial to ensure a clean shutdown. For explicit resource management,
Symbol.dispose
will offer a solution. When working with resources like database connections, where it is crucial to close the connection before program execution proceeds, this functionality is quite helpful. Here’s an example of use for Prisma:
Symbol.dispose
can be applied to other examples such as file handles, network connections and external service connections.

Conclusion

TypeScript has exciting features lined up on its roadmap, specifically related to ECMAScript Explicit Resource Management. These upcoming additions include
using
declarations,
Symbol.dispose
and a potential new interface called
Disposable
or
DisposableLike
. The proposal aims to enhance resource management capabilities in TypeScript, bringing it closer to the functionality seen in languages like C#. These advancements signify TypeScript's ongoing evolution and its commitment to providing robust resource management features akin to those found in other programming languages.

Would you like to innovate your ecommerce project with Hatimeria?

Author
Author Image
Dominik
Developer

He specializes in technologies such as Node, serverless and web/mobile applications. He is dedicated to expanding his skills and gaining hands-on work experience in the field. His biggest flaws are talkativeness, addiction to coffee and delicious food.

Read more Dominik's articles

Our Latest Thinking

Interested in something else?

Office

Meet the team

Learn more about company and the team.

Join Us

Join us

Make an impact on your career.