TypeScript 49 is now in beta, with significant updates including a new “satisfies” operator, but ECMAScript Decorators, which were planned for this release, are likely to be postponed. TypeScript 4.9 is expected to have a release candidate on November 1 and a final release mid-November.
“The new satisfies operator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression,” explains senior program manager Daniel Rosenwasser, in a post about the new beta, adding that “satisfies can be used to catch lots of possible errors.”
The satisfies operator is an oddity in some ways. It performs additional type checking without amending any types, which in some ways is what TypeScript already does with respect to JavaScript. The new operator, though, is a clever workaround for undesirable side-effects of changing the types themselves. “The satisfies operator is something I’ve been waiting for a long time. The style of type definitions I make have got me into the difficult territory more than once, where you want to have your type and eat it too,” says Snyk engineer Cefn Hoile in an explainer here.
There is plenty more in this release. TypeScript 4.9 disallows direct comparison with NaN (not a number), since in JavaScript nothing is ever equal to NaN, even NaN itself! Developers should use Number.isNaN but “lots of people accidentally end up checking with someValue == NaN instead,” says Rosenwasser.
Another new feature, this time in the tooling, is that using TypeScript’s watch mode, which automatically compiles files when they change, has been updated to use file system events by default, rather than polling for changes which is less efficient. It will still fall back to polling when necessary, but “for most developers, this should provide a much less resource-intensive experience,” Rosenwasser says.
There is one feature though which is not in 4.9, even though it was originally slated for inclusion. This is ECMAScript Decorators, a confusing topic since TypeScript already has experimental decorators, annotations which modify the behaviour of classes or class members. ECMAScript has its own decorator proposal, though, which has now reached Stage 3 in ECMAScript jargon, meaning it is feature-complete and awaiting final approval (Stage 4) to become part of the standard.
In a comment, Rosenwasser said that: “While decorators have reached stage 3, we saw some behavior in the spec that needed to be discussed with the champions. Between addressing that and reviewing the changes, we expect decorators will be implemented in the next version.”
The issue illustrates the complications of the relationship between TypeScript, which compiles to JavaScript, and JavaScript itself – JavaScript being an implementation of ECMAScript. TypeScript’s popularity among professional developers means that it is the primary language for many projects, but ECMAScript is also evolving, and their respective teams have to be careful to avoid introducing too many breaking changes.