rescript-lang.org Documentation Platform

repository·master·Indexed 23 days ago

https://github.com/rescript-lang/rescript-lang.org

The official documentation platform for the ReScript programming language. This pre-rendered static application is built with ReScript, React 19, React Router, Vite, and Tailwind CSS. The repository includes tools for managing compiler versions, validating markdown examples and links, and running component-level tests via Vitest and Playwright, as well as end-to-end tests using Cypress.

Tokens
115.3K
Snippets
497
Records
661
Agent score
82%

What's inside rescript-lang.org

  1. What is ReScript?

    master

    ReScript is a robustly typed programming language that compiles to efficient, human-readable, and performant JavaScript. It is designed for high-speed iteration with a lightning-fast compiler toolchain that scales to large codebases. Key features include:

    • Sound Type System: Guarantees that if a type is not marked as nullable, it will never be undefined, effectively eliminating null/undefined errors.
    • Type Inference: The language automatically infers types, so explicit type annotations are optional.
    • Optimized Output: Generates tiny, highly optimized JavaScript that leverages JIT optimizations (like hidden classes and inline caching).
    • Fast Iteration: Build times are significantly faster than many alternatives, with watcher mode often completing in milliseconds.
    • High-Quality Dead Code Elimination: Uses purity analysis and a well-engineered type system to eliminate unused functions and modules, and produces code that is friendly to bundlers like Rollup or Closure Compiler.
  2. Overview of ReScript & React integration

    master

    ReScript provides first-class bindings for ReactJS (compatible with React v18.0 and newer). The bindings are designed to compile to idiomatic JavaScript, making it easy to transfer React knowledge to ReScript and integrate with existing ReactJS codebases and libraries.

    Key features include:

    • Native JSX Support: No Babel plugins are required as JSX is built into the ReScript language.
    • Essential React APIs: Includes standard hooks like useState, useReducer, useEffect, and useRef.
    • Function Component Focus: The bindings do not support the component class API; all code is built using function components and hooks.
    • Type Safety: Provides strong type safety and type inference for component props and state values.
    • TypeScript Interop: Supports GenType for importing and exporting React components in TypeScript codebases.
  3. Understand the ReScript rebranding and ecosystem

    master

    ReScript is the unified brand for the technology previously known as BuckleScript and the JavaScript-focused parts of Reason.

    Key distinctions:

    • ReScript: The unified experience for JavaScript developers, including the compiler, build system, and the new ReScript syntax. It is optimized for JS interoperability and a fast, lean toolchain.
    • Reason: Continues to serve as a syntax layer for native OCaml. While you can still use Reason with Js_of_ocaml to output JS, the community focus has shifted to ReScript for JS-related development.
    • OCaml: The underlying typed FP language. ReScript's compiler continues to acquire relevant upstream OCaml features.
  4. Identify official ReScript communication channels

    master

    To ensure you are receiving accurate information and official news, only rely on the following channels. ReScript does not use Reddit, Discord, or Medium for official communication; announcements on those platforms may not represent the project's intent.

    Official News Channels:

    Official Community Channels:

  5. ReScript Syntax Overview

    master

    ReScript is a concise language where line breaks are sufficient to separate statements, so semicolons are not required. Core features include:

    • Variables: Use let for immutable bindings. For mutable values, use ref.
    • Strings: Must use double quotes ("). Supports concatenation with ++, interpolation with `...${}`, and tagged templates.
    • Booleans: Supports true, false, logical operators (!, ||, &&), and comparison operators (<=, >=, <, >, ===, !==, ==, !=). Note that === is for referential (shallow) equality and == is for structural (deep) equality. There is no implicit type casting.
    • Numbers: Supports integers and floats. Arithmetic operators (+, -, *, /, %, **) work for both types.
    • Null & Option: ReScript has no null or undefined. Use the option type: None for no value and Some(value) for a present value.
    • Functions: Supports anonymous functions (arg => retVal) and named functions (let named = (arg) => retVal).
    • Async/Await: Supports async functions and await for promises.
  6. Anticipated features in ReScript 13

    master

    ReScript 13 (targeted for Q4 2026) focuses on improving the developer experience through better debugging, faster builds, and closer alignment with JavaScript. Key improvements include:

    • Source Maps and Debugging: Support for linked, inline, and hidden source map modes to relate JavaScript stack traces and performance profiles back to ReScript source.
    • Faster, Observable Builds: Transition to the native Rewatch build system (replacing the legacy Ninja-based builder) using a work-stealing dependency-graph dispatcher. Build performance can be investigated via OpenTelemetry tracing (OTLP).
    • TypeScript Declarations: Direct .d.ts generation for ReScript-generated JavaScript to improve interoperability with TypeScript.
    • JavaScript-Style Control Flow: Addition of for...of and for await...of loops, including support for break and continue.
    • Language Features: Support for dictionary spreads, record rest destructuring, inline record types in external definitions, and first-class, type-safe tagged-template functions.
    • Breaking Changes: Removal of deprecated configuration, syntax, and standard-library APIs; default module system set to ES modules; minimum Node.js version raised to 22.
  7. What is a custom Hook and when to use one

    master

    A custom Hook is a function whose name starts with use that can call other Hooks. They allow you to extract existing component logic into reusable, separate functions. This is useful when you need to share stateful logic (like subscribing to an external API or managing complex state transitions) between multiple React components without duplicating code or adding extra components to the render tree via render props or higher-order components.

    let useFriendStatus = (friendId: string): state => {
      let (state, setState) = React.useState(_ => Offline)
    
      React.useEffect(() => {
        // ... logic
      })
    
      state
    }
  8. What is genType and how does it work?

    master

    genType is a code generation tool included in the ReScript compiler that enables interoperability between ReScript and TypeScript. It performs a type-directed transformation of ReScript programs after compilation, converting ReScript values and types into idiomatic TypeScript types.

    For example, a ReScript variant is transformed into a TypeScript union of objects with a TAG property:

    @genType
    type t = | A(int) | B(string)

    becomes:

    type t = { TAG: "A"; _0: number } | { TAG: "B"; _0: string };
  9. What is a Variant in ReScript

    master

    A variant is a data structure that allows you to express "this or that" (sum types), as opposed to standard structures that express "this and that" (product types). A variant is defined by a set of cases called "variant constructors" or "variant tags".

    Key Rule: Variant constructors must be capitalized.

    type myResponse =
      | Yes
      | No
      | PrettyMuch
    
    let areYouCrushingIt = Yes
  10. What is React Context and when to use it

    master

    React Context provides a mechanism to share data through the component tree without manually passing props down through every level (prop drilling).

    When to use Context

    Use Context for data that is considered "global" for a specific tree of components, such as:

    • Authenticated user information
    • UI themes (e.g., Light/Dark mode)
    • Preferred language/locale

    When to prefer Props

    In ReScript, because of JSX prop punning and strong type inference, passing props is often simpler and more transparent than in JavaScript or TypeScript. It is generally preferable to use standard props unless the data is truly global to a large tree, as it avoids the "magic" and complexity of Context.

  11. What is a React component in ReScript?

    master

    A React component in ReScript is a function that describes a UI element. It receives a props object (defined via labeled arguments) as a parameter and returns a React.element.

    To define a component, you must use the @react.component attribute and name the function make.

    @react.component
    let make = () => {
      <div>
        {React.string("Hello ReScripters!")}
      </div>
    }