Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting typescript erasable syntax #3587

Open
jasnell opened this issue Feb 21, 2025 · 0 comments
Open

Supporting typescript erasable syntax #3587

jasnell opened this issue Feb 21, 2025 · 0 comments

Comments

@jasnell
Copy link
Member

jasnell commented Feb 21, 2025

TypeScript erasable syntax is a fairly new subset of TypeScript that supports very fast, very minimal transformation to run as regular JavaScript simply by erasing the TypeScript annotations from the source. It is particularly beneficial given that it preserves line and column positions and does not rewrite any of the JavaScript parts making generation and use of source maps unnecessary.

The approach was pioneered by bloomberg in the ts-blank-space project where they were able to massively reduce the compile time of these TS files. Node.js has recently adopted the approach and can now run TypeScript using the erasable syntax profile directly without tsc, transpilation, etc.

Currently, workers itself is not directly able to support TypeScript. While many workers developers use TypeScript, we still require transpilation/compilation before the worker bundle is deployed. What's even more inconvenient, editing through the dashboard does not support the TypeScript syntax/transformation at all.

I think it would be worthwhile for us to explore adding direct support for TS erasable syntax in the runtime such that a deployed worker does not need to be transpiled. The removal of the TS syntax dynamically at runtime can be extremely fast.

To give an idea of what erasable syntax does... take this example from ts-blank-space:

class Cat<T> {
  public whiskers: number;
  public tail: T;

  constructor(count: number, tail: T) {
    this.whiskers = count;
    this.tail = tail;
  }
}

throw Error();

By erasing the TS annotations, this becomes:

class Cat    {
         whiskers        ;
         tail   ;

  constructor(count        , tail   ) {
    this.whiskers = count;
    this.tail = tail;
  }
}

throw Error();

Which, in human readable terms is ugly but is perfectly executable JavaScript.

Node.js' implementation of erasable syntax is built around Amaro/SWC. SWC is a very fast rust-based tool that does all of the heavy lifting here.

We obviously would need to verify that introducing this transform wouldn't unreasonably impact cold start times but given the benchmarks I've seen so far I would be surprised.

They key benefit that this would provide is that it would allow users to upload TypeScript workers directly, without transpilation. If those are edited in the dash they would continue to see the type annotations, making the editing experience more seamless. It would also mean that our own tests and built-ins could also take advantage of TypeScript annotations. There are limitations, of course, as not all TS is erasable (enum for instance) so it does not entirely eliminate the need for transpilation but it is a step in the right direction that the other runtimes and build tools are embracing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant