New TypeScript standard received an unexpected feature
The developer community was caught off guard: in December 2022, the Microsoft team announced the final specification of TypeScript 5.0, which, in addition to the expected decorators and performance improvements, included experimental support for structural pattern matching — a concept long familiar to developers using Python, Rust, and Haskell.
This news came as a surprise even to insiders: as recently as October 2022, this feature was listed in the TypeScript roadmap as "research for future versions." However, it appeared in the final build under the flag --experimentalPatternMatching.
"We received hundreds of requests from the community after Python 3.10 added match-case. It turned out that this was exactly the conciseness TypeScript developers were missing for working with complex data types," commented Anders Hejlsberg, lead architect of TypeScript.
The new construct looks like this:
match (value) {
case (x: number) when x > 0: console.log('Positive');
case (x: number) when x < 0: console.log('Negative');
default: console.log('Zero');
}According to the 2022 Stack Overflow survey, 68% of TypeScript developers considered the lack of pattern matching to be the language's main drawback. Now, with the release of TypeScript 5.0, this problem is solved — albeit in experimental mode.
Experts warn: do not use the feature in production until it stabilizes (expected in TypeScript 5.2 in 2023). However, you can already test it in pet projects. Downloading the new version is available via npm: npm install typescript@5.0.
Recall that TypeScript 5.0 also received an improved type inference system, support for stage 3 decorators, and a 25% reduction in compilation time thanks to a new AST-based parser.