Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • X
  • BlueSky
  • GitHub
  • Forum
Language Manual
Overview
  • Introduction
  • Installation
  • Editor Plugins
  • Migrate to ReScript Syntax
  • Try
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Async & Promise
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Unboxed
  • Reserved Keyword
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
Guides
  • Converting from JS
Extra
  • Newcomer Examples
    • Use the option type
    • Create a Parametrized Type
    • Creating a JS Object
    • Modeling a JS Module with Default Export
    • Checking for JS nullable types using the option type
  • Project Structure
  • FAQ
Docs / Language Manual / Newcomer Examples
Edit

Newcomer Examples

An example is worth a thousand words.

This section is dedicated to newcomers trying to figure out general idioms & conventions. If you're a beginner who's got a good idea for an example, please suggest an edit!

Use the option type

ReScriptJS Output
let possiblyNullValue1 = None
let possiblyNullValue2 = Some("Hello")

switch possiblyNullValue2 {
| None => Js.log("Nothing to see here.")
| Some(message) => Js.log(message)
}

Create a Parametrized Type

ReScriptJS Output
type universityStudent = {gpa: float}

type response<'studentType> = {
  status: int,
  student: 'studentType,
}

Creating a JS Object

ReScriptJS Output
let student1 = {
  "name": "John",
  "age": 30,
}

Or using record:

ReScriptJS Output
type payload = {
  name: string,
  age: int,
}

let student1 = {
  name: "John",
  age: 30,
}

Modeling a JS Module with Default Export

See here.

Checking for JS nullable types using the option type

For a function whose argument is passed a JavaScript value that's potentially null or undefined, it's idiomatic to convert it to an option. The conversion is done through the helper functions in ReScript's Js.Nullable module. In this case, toOption:

ReScriptJS Output
let greetByName = (possiblyNullName) => {
  let optionName = Js.Nullable.toOption(possiblyNullName)
  switch optionName {
  | None => "Hi"
  | Some(name) => "Hello " ++ name
  }
}

This check compiles to possiblyNullName == null in JS, so checks for the presence of null or undefined.

Converting from JSProject Structure

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on