A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://rescript-lang.org/blog/what-can-i-do-with-rescript below:

What can I do with ReScript?

You've taken a look and ReScript and you want to try it out, but how do you get started? There's the installation page in the docs, which is great if you want to set up a new React app using create-rescript-app. There's instructions on how to add it to an existing project or set it up manually. But that doesn't really answer the question "Can I use this with X?".

ReScript is just a language that compiles to JavaScript. Unlike other language like Elm or PureScript ReScript doesn't have a recommended framework or independent ecosystem, it's just part of the normal JavaScript world.

Here's a really basic example that you can run in Node after compiling:

Just run node index.res.js and you'll see "Hello" logged to the console. You can import compiled ReScript into any project that could import JavaScript. If you can use .js or .mjs files, you can use ReScript. This does mean that languages with different file formats like Vue or Svelte require you to import the compiled JavaScript instead of writing it directly in the .vue or .svelte files.

Real world projects are more than JavaScript files that you write; they use libraries and frameworks. This is where bindings come into play. A binding is a way to tell ReScript the types and imports from external JavaScript. You can think of bindings in the same way that you need to create a *.d.ts file to add types to a JavaScript library that doesn't use TypeScript.

ReScript has great integration with React and those bindings are kept up to date by the core team, but that doesn't mean you don't have other options!

While ReScript isn't as large as TypeScript it has a small but growing list of bindings you can find on NPM. The website has a package explorer you can use to find official and community maintained bindings. Many major libraries have existing bindings. Here's a small set of what you can find.

Bindings are great if you want to work with libraries written with JavaScript, but there are great options for libraries and frameworks written with ReScript, which means you don't need bindings.

At some point you will probably have to use a library that doesn't have bindings available. Asking on the forum is a great place to start. Someone else might have bindings already in a project that they just haven't published to NPM. You can also get help and guidance on how to write bindings for what you need. Usually you can figure out what you need from looking at a libraries official docs. You don't need to write bindings for an entire library, or even for all of a functions arguments. Just write what you need as you go.

Let's take a look at the format function from date-fns. We can see the arguments in the docs, and how it should be imported and used.

TS

function format( date: string | number | Date, formatStr: string, options?: FormatOptions ): string import { format } from "date-fns"; const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy')

That's all we need to know to write bindings to use this function in ReScript. The first thing we need to figure out is how to handle the type for what date-fns considers to be a date, which is Date | string | number. In ReScript things can't just be of different types like they can in JavaScript or TypeScript. There are a couple options here; you can make a function for each type such as formatString and formatDate, or you can create a variant type to map to the possible input types. Creating a function for each type is simpler, and it's most likely how you will use the library in your project. You probably have a standard type for Dates already. We'll also need a type for FormatDateOptions in case we want to pass options. We'll use labeled argmuments for our binding.

RES

type formatDateOptions @module("date-fns") external formatString: ( ~date: string, ~formatStr: string, ~options: formatDateOptions=?, ) => string = "format"

Now we can use the function!

let formattedDate = DateFns.formatString(~date="2021-09-01", ~formatStr="MMMM dd, yyyy")

If we need to use FormatDateOptions we can add to our type definition as needed. The first option is firstWeekContainsDate which can either be 1 or 4. Here's how we could write bindings for that.

RES

@unboxed type firstWeekContainsDate = | @as(1) One | @as(4) Four type formatDateOptions = {firstWeekContainsDate: firstWeekContainsDate}

And when we use it it will output either 1 or 4.

let formattedDate = formatString(
  ~date="2021-09-01",
  ~formatStr="MMMM dd, yyyy",
  ~options={firstWeekContainsDate: Four},
)

You can write new bindings and extend existing types as you need.

You can follow this guide to add ReScript to an existing JavaScript project to get a feel for how the language works and interacts with JavaScript. The forum is also a great place to ask questions! Feel free to drop by and ask how to get started with a specific framework or project that you want to work on, and you'll probably get great advice and information from users who have already used ReScript for something similar.

Happy coding!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4