TypeScript 5.1 released, 5.2 beta on way with resource management on the roadmap – more like C#?

TypeScript 5.1 released, 5.2 beta on way with resource management on the roadmap – more like C#?

Microsoft has released TypeScript 5.1, with improvements including less fuss over functions that return void or undefined, and allowing properties to have unrelated types for setting and getting. The 5.2 beta is expected later this month, with a major new feature on the roadmap: a “using” keyword that is reminiscent of C#.

TypeScript 5.1 has around 8 key new features, according to Senior Program Manager Daniel Rossenwasser, though none are huge changes.

One that will make life easier for devs is that “TypeScript 5.1 now allows undefined-returning functions to have no return statement.” The point here is that TypeScript used to insist that a function had a return statement, even if the return value was declared to be undefined, or in other words what you would get in JavaScript if the function did not have a return statement at all. “Understanding the interplay between inferring void over undefined, whether an undefined-returning function needs a return statement, etc. seems like a distraction,” said Rossenwasser, stating that 5.1 brings an end to this “frustrating and confusing” behavior.

Another change is TypeScript 5.1 no longer requires that the get type of a property is a subtype of that used for set. It might seem intuitively that properties should use the same type both when setting or getting the value; but in practice they do not. An example is the CSSStyleRule API which can be set only with a string, but returns a value of the type CSSDeclarationStyle. This will now be possible in TypeScript.

Several changes in the new version improve support for React’s JSX components.

A potentially bigger change is coming in the next version of TypeScript, 5.2. The C# language has a “using” statement that is designed for objects that consume native resources such as file handles and means that they get cleaned up automatically. Types that support this must implement an interface called IDisposable. JavaScript lacks any such mechanism, but this may be coming to the language thanks to a proposal for explicit resource management in the TC39 group which specifies ECMASCript (JavaScript). TypeScript compiles to JavaScript. The proposal notes as “prior art” the C# “using” statement, the Java “try-with-resources” statement, and the Python “with” statement. This area has become more relevant with the increasing capabilities of JavaScript including its use outside the browser.

TypeScript 5.2 therefore has on its roadmap ECMAScript Explicit Resource Management & using Declarations including “using” declarations, Symbol.dispose and “possibly a new interface called Disposable (which I think we should consider calling DisposableLike.” The proposal adds that “it would be reasonable for our first stab at this feature to include only support for –target esnext,” referencing the forthcoming as opposed to the current version of ECMAScript. TypeScript therefore could become a little more like C#.

The first beta of TypeScript 5.2 is scheduled for June 27th.