DEV Community

Cover image for Tired of Typescript? Check out ReScript!

Tired of Typescript? Check out ReScript!

Josh Derocher-Vlk on April 19, 2024

If you've spent any amount of time developing with JavaScript you have probably found it difficult to maintain a large code base and avoid runtime ...
Collapse
 
efpage profile image
Eckehard

Sounds great. How is the VScode-integration of ReScript? Can wen enable syntax checks while typing?

Collapse
 
dzakh profile image
Dmitry Zakharov

There's an incremental type-checking recently added in the VSCode extension. You can enable it by adding this in your VSCode settings:

"rescript.settings.incrementalTypechecking.acrossFiles": true,
"rescript.settings.incrementalTypechecking.enabled": true,
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zth profile image
Gabriel Nordeborn

Great article, thank you!

Collapse
 
efpage profile image
Eckehard

How would ReScript deal with pure Javascript code? Assume you have a large number of libraries. Ideally I would like to incrementally add type annotations to the code, while untouched code is still running.

Is this expectation too idealistic?

Collapse
 
jderochervlk profile image
Josh Derocher-Vlk

ReScript has a way to bind to any JS library or file: rescript-lang.org/docs/manual/late...

It's pretty easy to do once you grok the concept, and it's much simpler than JS interop in other languages like Elm or F#.

You would convert over one file at a time, or even split out functions or components into a new file. I've migrated over a couple projects and I've found it easy to start with the smaller parts of an app, like hooks and functions, and the work up to components. The ReScript code can have bindings to the JS code, and the JS code can import from the compiled ReScript.

Collapse
 
jderochervlk profile image
Josh Derocher-Vlk

I did a walkthrough here if you want to see an example: dev.to/jderochervlk/converting-a-j...

Collapse
 
efpage profile image
Eckehard

But how much changes would a library need to migrate from JS to ReS? As far as I understood native code should be widely compatible with ReScript?

Thread Thread
 
jderochervlk profile image
Josh Derocher-Vlk

It depends, but unlike TypeScript it's more of a process than renaming the file and seeing it compile. I tend to make rescript file to start moving over a component or functions into one by one and importing those functions back into JS land as I go.

My steps are usually:

  1. remove imports since rescript doesn't have imports, everything has to be in a rescipt code or have bindings somewhere. Depending on how many external libraries you use this might be easy or take some time, but once you have bindings for the core dependencies it goes quicker. Of course major libs like React already have good bindings for rescript you can find on npm
  2. fix all the declarations to use just let binding. Rescript doesn't have var, const, or function declarations. You can usually do this with a search and replace.
  3. Remove any returns. Rescript returns whatever the last expressions is, so you don't have/need a return keyword
  4. Make sure you have types for any objects you might be using. This again might take longer for the first time in a repo, but usually once you have the key ones defined it becomes easier

I've done a half dozen migrations from JS/TS to ReScript and it usually takes me a day of work, one took me 3 days because it was really 2 websites with a heavy dependency on external libraries that were a pain to create types and bindings for.
I've always been really satisfied once I have a project running on Rescript.

I've also converted many thousands of lines of non-strict typescript to strict typescript, and I can say that the migration is much easier to move to ReScript.

Thread Thread
 
efpage profile image
Eckehard

Thank you for your valuable feedback. It seems, it´s more than just adding some type declarations. Some of the JS concepts (specially the ES6-module system) are really quirky, so it is probably a good decision to go in a different direction.

I´m just not too happy about what might happen in the future. JS will keep on moving, so it is an important question if a language like ReScript (and even typescript) can keep up with this development. It's easy to get onto a dead track if JS does some bigger steps.

Thread Thread
 
jderochervlk profile image
Josh Derocher-Vlk

The majority of what I've done with ReScript has been ESM compatible with Vite and node 20. The good thing about working in a language that compiles to JS is that if something in JS changes, it doesn't mean your rescript code has to change. The compiler can always be updated to output different JS if needed.

One of the things that makes JS a terrible language to work with, but a great runtime target, is that nothing ever gets removed or deprecated from the language. So if your JS or Rescript works today, it will work in the future.

Collapse
 
fhammerschmidt profile image
Florian Hammerschmidt

Only twice as fast as TypeScript? Does not sound right to me, but it of course extremely depends on what dependencies you have installed because some really tend to bring the (turing-complete) type system of TS to its limits.

Collapse
 
jderochervlk profile image
Josh Derocher-Vlk • Edited

This is with 32k TypeScript vs 50k rescript, so it's more than twice as fast. It's not that complicated of a typescript project, just react and some basic libraries. I have a small typescript project with a couple thousand files that takes almost 5 minutes to type check because it has a lot of overloaded function declarations and generic types.

You can optimize typescript to be fast by doing things like making sure functions have return types, but it's something you have to really work at. ReScript is just fast out of the box.

Full builds taking half the time TypeScript does is great, but the biggest win to me is gradual type checking while I work being almost instant. I can open up a file in VSCode and see type hints the moment I hover around the code. I save and get feedback about a broken type in another file in under a second.

Collapse
 
coder_one profile image
Martin Emsky

Is it currently possible to use data-attributes inside JSX in Rescript?

Collapse
 
jderochervlk profile image
Josh Derocher-Vlk • Edited

Yes. There are a few already available in rescript-react like <div dataTestId="foo" /> which compiles out to <div data-testid="foo"/>.

If you want something custom you do have to define a custom JSX transform with the data-atrributes you want to have with the types they accept. You can do this per file, or ideally you'll want to do it once for an entire project.

Here's a forum post with some details on how to set it up: forum.rescript-lang.org/t/custom-d...

Collapse
 
insign profile image
Hélio oliveira

Dart exports to js

Collapse
 
u007 profile image
James

look like php lol -> omg