[PROPOSAL] Integrate Napi.rs to TSC to gradually migrate to Rust 🦀 #60480
+456
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is to contribute a suggestion, showcasing a working code example. It is not meant to be merged.
The web development ecosystem has evolved a lot, moving towards faster technologies, usually Rust or Golang. For example, Turbopack, Rspack, swc or esbuild (already used in this repo).
Unfortunately, those projects can only compile Typescript, not type check. Type checking remains a problem in large projects due to its performance.
There have been attempts at rewriting the TSC type checker in Rust, mainly by the well known @kdy1, creator of SWC, with the https://github.com/dudykr/stc project. However, keeping up with the Typescript changes is no easy task.
For this proposal, I use NAPI-RS directly in the Typescript repo, allowing us to gradually migrate the codebase to Rust. This is of course easier said than done, but I believe if the hot paths were migrated, it could already give a great performance boost.
This example is fully working, as you can see all the checks passing. I have only migrated 2 methods,
escapeLeadingUnderscores
andunescapeLeadingUnderscores
, for the sake of demonstration. They are implemented inlib.rs
, and runningnpm run build:napi
will creaters.js
andrs.d.ts
files, allowing JS code to call them directly.I'd love to hear your thoughts with such a solution. If there is interest, I can open another PR which would integrate it properly (using Hereby) and have all CI workflows passing.
PS: 2 CI workflows are failing, mostly due to configuration that needs to be tweaked, but
npm test
passes successfully for Linux, Windows, and MacOS.