10/21/2021

The problem with Typescript

Typescript is an amazing project, but a perplexing open-source blackbox


Typescript has one of the most unique type systems. Because the language is made to transpile down to an already existing language made very fast by projects like V8, all of the language's design and upkeep can focus on types. That coupled with the need for a type system to model the dynamicism of JavaScript is what makes Typescript's type system the most powerful and expressive one I've ever encountered.

There's one big caveat though, and it's the sheer complexity of the Typescript project. The type checker itself is Typepscript's largest component and it's a single 25 thousand line file.

The project is so big I get the sense that it's the open source equivalent of a black box. No one besides Microsoft really understands how it works, and even Microsoft themselves have said the project is so complicated that optimizations are blocked by complexity.

And this is kind of bad because it means that Typescript is 100% in the hands of Microsoft, and I don't mean this in the anti-corporate sense. I mean this in the simple sense that if anyone has an idea to improve the project, or build off of it, the only way it can be made a possibility is if it goes through Microsoft and they implement it. Microsoft has their own goals for the project, and they cannot accommodate every feature request and use-case.

The primary problem is tsc is slow. Deno has addressed this as the TSC bottleneck, and it's a result of the fact that Typescript's type system is inherently complex, and written in JavaScript. Projects like esbuild, swc, or Rome can transpile Typescript incredibly fast, but they simply strip the code of its type information. They can't typecheck, so our build systems are inevitably blocked by waiting on TSC.

This doesn't just affect us at compilation time, it also affects us at development-time. tsserver can have trouble keeping up with big projects, not to mention the memory requirements of running it. VSCode has good language support for Typescript, and it's no surprise since it's made by Micrsoft, but you'll experience slow downs if using another editor.

So where do we go from here? The swc project is actively working on a type-checker in Rust, but it's closed-source and they plan on selling it.

It seems Deno and Rome tools are discussing ways to tackle the problem, but they seem far from having something ready.

esbuild's author Evan Wallace has said that type-checking is out of scope for the project.

Microsoft says they have no plans to port the Typescript compiler to Rust, and I get the sense that they have a different vision for Typescript.

ReScript, Elm and PureScript are languages with great type systems that transpile to JavaScript and have much faster compilation times. However, they come at the cost of having a far less mature ecosystem/community.

Update: I'm working on a Typescript type-checker of my own, tyty


Got any questions? Did I say something completely bogus and wrong? Hit me up on Twitter.