JavaScript Set methods now usable cross-browser, and in newly released TypeScript 5.5

JavaScript Set methods now usable cross-browser, and in newly released TypeScript 5.5

JavaScript Set methods – an addition to the ECMAScript standard now declared ready to ship – are fully supported across major browsers and in the newly released TypeScript 5.5.

According to Mozilla’s Brian Smith, the release of Firefox 127 earlier this month means that these methods “are available in most major browser engines, which means you won’t need a polyfill to make them work everywhere.”

A JavaScript Set is similar to an array except that each value must be unique. Adding a value to a Set is conditional on it not already being present. The new Set methods include union(), which combines two sets into one new set, and intersection(), which returns a new set with all the values that are in both of two existing sets. The difference() method has all values that are in a target set, but not in a specified other set, while the symmetricDifference() method has all of the values in one but not both of two existing sets. There are also methods for isSubsetOf(), isSupersetOf() and isDisjointFrom().

It takes a long time for new JavaScript features to become usable without polyfills (library code to implement language features that would otherwise be unavailable). The Set methods proposal was discussed in a 2018 technical commitee meeting, when it was split from another proposal that also included collection methods. Part of the rationale is that other languages – including Python, Ruby, Swift, Rust and C# – have similar methods and there is high value in being consistent.

Making progress

The Set methods are at Stage 3 of the ECMAScript standard process. That means the proposal is complete, unless there is a significant push for amendment. Stage 4 means finished. The main activity needed for Stage 4 is implementation in major browsers – so it is likely that this will follow soon. The MDN (Mozilla Developer Network) compatibility chart shows support in the latest version of all common browsers other than Samsung Internet, including Demo and Node.js for server-side code.

TypeScript 5.5, which has just been released, did not support the Set methods in beta, but this was added at Release Candidate stage. Given the popularity of TypeScript, its inclusion now is significant. Language designer Kevin Gibbons both championed Set methods in ECMAScript and also provided the declarations for TypeScript, according to Microsoft principal product manager Daniel Rosenwasser.

The new TypeScript also includes Regular Expression syntax checking, a refined type checker, support for @import comment tags, and other changes detailed in Rosenwasser’s post. It also semi-disables features deprecated in TypeScript 5.0, meaning that they can still be specified in the TypeScript configuration file tsconfig, but only until the release of TypeScript 6.0.