TypeScript is not TypeScript anymore...

BBetter Stack
Computing/SoftwareInternet Technology

Transcript

00:00:00typescript just put out a release candidate for version 7 and this is going to be the version where
00:00:04typescript is not typescript if you've been out of the loop they've been working on rewriting the
00:00:07typescript compiler from typescript itself to go and apparently the results are 10 times faster
00:00:12they expect to release typescript 7 in the next month so let's go through what's actually changed
00:00:17how fast it is and if there's any breaking changes that you need to know about before you install it
00:00:26so if you missed the news about them porting to go they actually started this about a year ago
00:00:29and the tldr is that they realized javascript was never built for the heavy cpu bound work that a
00:00:34type checker does so they began rewriting it into go to great early success they actually started by
00:00:39basically porting the existing implementation of typescript essentially line by line so the type
00:00:44checking logic was all structurally the same and had the same behavior and you could even see some
00:00:48of the functions were nearly identical besides the language i'm also pretty sure that this was before
00:00:52you could just point claude at your code base and say migrate to whatever language you want
00:00:56i'm looking at you bun the results of the port actually speak for themselves here i have the
00:01:00playwright repo and if i do a type check using the old version of typescript here we can see this
00:01:04takes around six seconds to complete and it went through 1400 files and half a million lines of
00:01:08code if i now switch over to the release candidate without changing anything except this command
00:01:12in total it took 0.87 seconds that is a serious improvement it also found the exact same amount of
00:01:18errors the same errors it went through the same files and all the lines of code so it works exactly the
00:01:23same as typescript 6. the native code of go is just fundamentally quicker than javascript for a
00:01:27task like this but it also allows them to use shared memory parallelism so where the javascript compiler
00:01:32was single threaded go can actually spread that type checking across multiple cores at once in typescript
00:01:377 you can actually enforce it to be single threaded with a flag so maybe you're doing some debugging
00:01:41or you're running on a machine with limited resources and if i do this in the playwright code base
00:01:46here with typescript 7 we can see that when single threaded this takes around two seconds which is
00:01:50still three times faster than what it was before talking of running in parallel they're also exposing
00:01:54a new checkers flag which actually lets you set how many type checker workers can run in parallel
00:01:58and this defaults to four increasing this could speed up your builds on larger code bases if you
00:02:03have a lot of cpu cores but it will come at the cost of extra memory usage if i set the checkers
00:02:08to a in this playwright repo which is double the default it does actually seem to shave another
00:02:12third of the time there's also a new builders flag for paralyzing project reference builds aka building
00:02:16multiple projects at once and this flag lets you control the number of parallel builders that can
00:02:20run at once and it's worth noting that if you combine this with checkers that we just saw let's
00:02:24say you had four of each that means you can have up to 16 type checkers running at once now besides
00:02:29the native code changes and parallelism another big rewrite in typescript 7 has been its watch mode
00:02:34when they ported to go this was actually a little trickier as the standard library doesn't provide
00:02:38built-in file watching apis and the third-party libraries they tried had issues with things like
00:02:43stability performance and cross-platform support so the team actually looked at the parcel bundlers
00:02:47file watcher which microsoft actually uses a bit off in vs code but since it was in c++ they also
00:02:53had to port the parts of that that they needed into go as well the good news is though that they did
00:02:57everything and it seems to be working really smoothly and better than before next though since this is
00:03:01a major version bump you might be expecting a lot of breaking changes especially since this is a big
00:03:05rewrite but i don't actually think there are any if you're upgrading from typescript 6 to 7. if you
00:03:10want to go from 5 to 7 there is going to be quite a few so it seems that they're recommending that you
00:03:14go up to 6 first get all of that working and then the version bump to 7 should be no problem a few of
00:03:19the big changes in typescript 6 were removing the es5 target removing base url and deprecating module
00:03:24systems amd umd and system js they also made strict true by default they made module default to esnext
00:03:31and target defaults to the current stable ecmascript version immediately preceding esnext it was
00:03:36basically a lot of leaving the past behind and modernizing typescript which i really like as
00:03:40sometimes trying to support legacy projects in every single version you make can really slow down the
00:03:45progress of a tool looking through the rest of this blog post it actually seems that the only new
00:03:49feature or change that actually concerns the typescript language itself is that template literal
00:03:53types now preserve unicode code points essentially before typescript 7 typescript actually split on utf 16 code
00:03:59units so it would end up splitting an emoji in half and you'd end up with these weird types for head
00:04:04and tails here in typescript 7 however it actually splits on whole code points so complete characters
00:04:09so now the emoji is preserved and the split is pretty much as you would expect it to be i would
00:04:13honestly be incredibly impressed if any of you have ever come across this in your time using typescript
00:04:18all in all these changes should make anything that uses typescript feel much faster like typescript
00:04:22in your editor especially for large projects the stable release is expected within about a month
00:04:27but a stable programmatic api so the thing that tooling authors use to build on top of the compiler
00:04:32is landing in version 7.1 because of this there is also a compatibility package so you can run
00:04:36typescript 6 and 7 side by side without running into conflicts let me know what you think of all of
00:04:41this and i'm curious if you've ever felt that typescript was feeling slow let me know in the
00:04:44the comments while you're down there subscribe and as always see you in the next one

Key Takeaway

By porting the compiler from JavaScript to Go and implementing shared memory parallelism, TypeScript 7 reduces type-checking times by up to 10x while maintaining full compatibility with version 6.

Highlights

  • TypeScript 7, currently in release candidate, uses a compiler rewritten in Go, resulting in 10x faster performance compared to the previous JavaScript-based version.

  • Type-checking the Playwright repository, containing 1,400 files and 0.5 million lines of code, dropped from 6 seconds to 0.87 seconds with the version 7 release candidate.

  • TypeScript 7 enables shared memory parallelism, allowing type checking to utilize multiple CPU cores instead of the previous single-threaded JavaScript execution.

  • New configuration flags include 'checkers' to control parallel type-checker workers (defaulting to 4) and 'builders' to manage parallel project reference builds.

  • Template literal types in version 7 now preserve Unicode code points, correctly handling emoji splits that previously resulted in broken types.

Timeline

Compiler Rewrite to Go

  • The TypeScript compiler is rewritten in Go to handle CPU-bound type-checking tasks more efficiently.
  • Performance benchmarks on the Playwright repository show a reduction in type-checking time from 6 seconds to 0.87 seconds.
  • The new implementation produces the identical error output and behavior as TypeScript 6.

JavaScript was not originally designed for heavy CPU-bound operations required by a type checker. By rewriting the compiler in Go, the team achieved significantly faster execution speeds. Testing with the Playwright repository, which includes 1,400 files and 500,000 lines of code, confirms the performance gain remains consistent across large codebases.

Parallelism and New Configuration

  • TypeScript 7 utilizes multi-core processing, moving away from single-threaded execution.
  • A 'checkers' flag allows users to define the number of parallel type-checker workers, defaulting to 4.
  • A 'builders' flag enables parallel processing for project reference builds.

Go allows for shared memory parallelism, enabling the compiler to distribute work across multiple CPU cores. While a single-threaded mode exists for resource-constrained environments, multi-core utilization further decreases build times. Users can increase the 'checkers' and 'builders' flags to scale performance, though this incurs higher memory usage.

Watch Mode and Upgrade Path

  • The file-watching implementation for watch mode was ported from C++ to Go to ensure cross-platform stability and performance.
  • No breaking changes exist when upgrading from TypeScript 6 to 7.
  • Upgrading from TypeScript 5 requires an intermediate upgrade to version 6.

Integrating file-watching into the Go-based compiler required porting C++ code from the Parcel bundler to maintain performance and cross-platform support. Version 7 streamlines the upgrade process by recommending users update to version 6 first, which contains the bulk of breaking changes such as removing ES5 targets and deprecating legacy module systems like AMD and UMD.

Language Features and Compatibility

  • Template literal types now split on whole Unicode code points rather than UTF-16 code units, preserving emojis.
  • Version 7.1 will introduce a stable programmatic API for tooling authors.
  • A compatibility package allows TypeScript 6 and 7 to run side-by-side during transitions.

The only significant change to the language itself is the improved handling of Unicode in template literal types, preventing character splitting errors. To support tooling developers who rely on the compiler API, a compatibility package is provided for side-by-side usage until the stable API lands in version 7.1.

Community Posts

No posts yet. Be the first to write about this video!

Write about this video