New Features in TypeScript 5.2: Explicit Resource Management, Decorator Metadata, and Tuple Enhancements
TypeScript 5.2 beta introduces explicit resource management with a new using keyword, adds decorator metadata via context.metadata, and enhances tuples by allowing mixed labeled and unlabeled elements while improving union array type checking, all available now through npm or the VS Code nightly extension.
TypeScript 5.2 beta has been released, allowing early access to new features such as the explicit resource management proposal, decorator metadata, and mixed‑named tuples.
Install the beta version with $ npm install typescript@beta or use the "JavaScript and TypeScript Nightly" VS Code extension.
The TC39 proposal proposal-explicit-resource-management (stage 3) introduces a using keyword that automatically disposes resources when a block ends. Example:
function* g() {
using handle = acquireFileHandle(); // resource bound to block
}
{
using obj = g();
const r = obj.next();
} // handle is released automaticallyIn Node.js, the same pattern works with synchronous and asynchronous file handles:
{
using handlerSync = openSync();
await using handlerAsync = openAsync(); // async support
}When the using keyword is compiled by Babel, it is transformed into a try/finally pattern that calls babelHelpers.dispose :
try {
var _stack = [];
var handlerSync = babelHelpers.using(_stack, openSync());
var handlerAsync = babelHelpers.using(_stack, await openAsync(), true);
} catch (_) {
var _error = _;
var _hasError = true;
} finally {
await babelHelpers.dispose(_stack, _error, _hasError);
}Decorator metadata (stage 3) is now supported in TypeScript 5.2 via context.metadata . The metadata object is merged into [Symbol.metadata] on the class, enabling inheritance and overrides.
function meta(key, value) {
return (_, context) => {
context.metadata[key] = value;
};
}
@meta('a', 'x')
class C {
@meta('b', 'y')
m() {}
}Tuples in TypeScript 5.2 can mix labeled and unlabeled elements, e.g.:
const arr: [name: string, number, boolean] = ['linbudu', 18, true];TypeScript also improves type checking for union array types. For string[] | number[] , methods like filter now accept a parameter of type string | number and return Array .
declare let array: string[] | number[];
array.filter(x => !!x); // parameter type: string | numberDaTaobao Tech
Official account of DaTaobao Technology
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.