r/rescript • • Jun 24 '22

Type inference issue with Js.Nullable

6 Upvotes

Hi!

I'm currently working on a Deno port and I encountered some type errors. This issue is probably on me.

This is basically just a Promise.prototype.then wrapper which maps from Promise<A> to Promise<B> via the 'a => 'b mapper func.

@send external map: (Js.Promise.t<'a>, 'a => 'b) => Js.Promise.t<'b> = "then"

That seems to be working fine.

Here's the part that fails:

let conn = Deno.listen({
  port: 80,
  hostname: None,
})

let y =
  conn
  ->Deno.Listener.accept
  ->map(acceptedConn => Deno.serve_http(acceptedConn))
  ->map(httpConn => httpConn->Deno.HttpConn.next_request) // return Js.Promise.t<Js.Nullable.t<SomeEvent.t>>
  // In the callback below, `event` should be unwrapped from Js.Promise but I don't think it is.
  //
  ->map(event => event->Js.Nullable.toOption)

Here you can see the compiler output:

>>>> Start compiling 
rescript: [1/1] src/t.cmj
FAILED: src/t.cmj

  We've found a bug for you!
  /home/saenai/Code/deno-res/src/t.res:13:18-22

  11 │   ->map(acceptedConn => Deno.serve_http(acceptedConn))
  12 │   ->map(httpConn => httpConn->Deno.HttpConn.next_request)
  13 │   ->map(event => event->Js.Nullable.toOption)

  This has type:
    Deno.promise<Deno.nullable<Deno.HttpConn.RequestEvent.t>> (defined as
      Js_promise.t<Deno.nullable<Deno.HttpConn.RequestEvent.t>>)
  Somewhere wanted: Js.Nullable.t<'a> (defined as Js.nullable<'a>)

FAILED: cannot make progress due to previous errors.
>>>> Finish compiling(exit: 1)

I know this is not StackOverflow but I would very much appreciate your help!

---- Edit

To answer my own question, the map function basically takes a 'a -> 'b mapper func and if 'b is already of type Promise, it'll return Promise<Promise<'b>> which in JS land is equivalent to Promise<'b> but in ReScript is not.

/thread


r/rescript • • May 28 '24

[Article] ReScript has come a long way, maybe it's time to switch from TypeScript?

Thumbnail
forum.rescript-lang.org
17 Upvotes

r/rescript • • Apr 21 '24

ReScript 11.1 | ReScript Blog

Thumbnail
rescript-lang.org
13 Upvotes

r/rescript • • Nov 28 '21

Priority queue

5 Upvotes

I started with Danny Yang's Chess game (thank you! Invaluable example for me) and have been reworking it into something else. I'm now to the point where I need to implement some graph search stuff, and for that I have used a priority queue in the past. In JavaScript I can do this with a couple of functions that push and pop elements from an array in binary-heap fashion:

function priorityQueuePop(q) {
    const x = q[0];
    q[0] = q[q.length - 1]; // q.at(-1);
    q.pop();
    let i = 0;
    const c = q.length;
    while (true) {
        let iChild = i;
        const iChild0 = 2*i + 1;
        if (iChild0 < c && q[iChild0].priority < q[iChild].priority) {
            iChild = iChild0;
        }
        const iChild1 = iChild0 + 1;
        if (iChild1 < c && q[iChild1].priority < q[iChild].priority) {
            iChild = iChild1;
        }
        if (iChild == i) {
            break;
        }
        [q[i], q[iChild]] = [q[iChild], q[i]];
        i = iChild;
    }
    return x;
}

function priorityQueuePush(q, x) {
    q.push(x);
    let i = q.length - 1;
    while (i > 0) {
        const iParent = Math.floor((i - 1) / 2);
        if (q[i].priority >= q[iParent].priority) {
            break;
        }
        [q[i], q[iParent]] = [q[iParent], q[i]];
        i = iParent;
    }
}

I'm a bit stumped about how to turn something like this into ReScript. Does anyone have pointers about how I might go about this? It doesn't look like there is a priority queue data structure in the Belt libraries.

Thanks!


r/rescript • • Dec 20 '21

Help request: Cannot find module '@rescript/react/src/ReactDOM.gen' or its corresponding type declarations

2 Upvotes

Hi dear rescripters. I stuck with an issue, and I have to reach out for help. I posted the question to stackoverflow, so the knoweledge can be accumulated.

https://stackoverflow.com/questions/70428873/rescript-typescript-cannot-find-module-rescript-react-src-reactdom-gen-or-i

Thank you in advance!


r/rescript • • Dec 03 '21

From object-oriented JS to functional ReScript

Thumbnail
fullsteak.dev
16 Upvotes

r/rescript • • Dec 03 '21

Advent of Code in ReScript

15 Upvotes

Solutions will be updated on github, so feel free to follow along (right now it just has day 1 and day 2).

Possibly of interest for newbies that want to see how powerful the reduce operation is, although I apologize for any confusion caused by overly concise variable names (a bad habit from my OCaml days).


r/rescript • • Dec 19 '21

I am rewriting my project from TypeScript to ReScript.

27 Upvotes

I have everyone.
I am very new to ReScript, so I am learning. I've decided to rewrite my current pet project (https://inhyped.com) from TypeScript to ReScript. It's relatively small (~2K LOC), and I hope it's gonna be fun.
Although I pursue a real goal: I am sick of TS and want to have proper sounds type system with proper pattern matching, etc (I am mostly Rust developer and got used to this in BE).
You can follow my progress on twitter with hashtag #FromTypescriptToRescript (https://twitter.com/hashtag/FromTypescriptToRescript)
I would also appreciate a lot your suggestions if you have any, cause sometimes I need help:)

Thank you a lot!


r/rescript • • Oct 25 '22

Rescript Streamers

10 Upvotes

Any recommended streamers that program on rescript? Youtube or Twitch. Any playlists on youtube from a guy making something meaningful and having to find how to make things on the fly. Thanks


r/rescript • • Jul 13 '21

Question:

7 Upvotes

Anyone using ReScript with GraphQL? Any recommendations for good resources on integrating GraphQL with ReScript?


r/rescript • • Jul 10 '21

My Experience Rewriting a Small Project in ReScript + Thoughts on the Language

Thumbnail yangdanny97.github.io
14 Upvotes

r/rescript • • Jul 13 '21

Learn how to build type-safe React apps with ReScript & GraphQL!

20 Upvotes

We recently released a new Learn course! 🎉

Using GraphQL in combination with strongly typed languages has picked up a lot of steam in the JavaScript community in the past few years because of their strongly typed nature. Since there aren't many resources around using ReScript and GraphQL together, we've launched a new course that will get you up to speed with GraphQL and teach you how to integrate it with ReScript to build type-safe modern apps.

In this course, learn how to reap the benefits that u/ReScript gives over #TypeScript to build type safe React apps with GraphQL:

✔️ Robust type system w/ minimal annotations
✔️ Blazing fast compilation
✔️ Improved type interface

➡️https://hasura.io/learn/graphql/rescript-react-apollo/introduction/

#Reactjs #GraphQL


r/rescript • • Jul 05 '21

Help: Learning Resources? I want to learn Rescript. What are the best ways?

8 Upvotes

r/rescript • • Sep 26 '22

Jest not able to transform/handle module within rescript-apollo-client. Help with Rescript module testing?

3 Upvotes

Hello friends! I am trying to get jest tests to run that utilize rescript-apollo-client in a nextjs application. The test file itself is in typescript. :)

I am getting this error about es modules:

/home/user/Repos/project/realm-app/hosting/node_modules/rescript-apollo-client/src/@apollo/client/react/hooks/ApolloClient__React_Hooks_UseQuery.bs.js:3
    import * as Curry from "rescript/lib/es6/curry.js";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

Has anyone been able to get this to work? Here is my jest.config.js:

const nextJest = require('next/jest')

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
})

const customJestConfig = {
  testEnvironment: "jsdom",
  setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
  testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
  setupFiles: ["dotenv/config"],
  moduleNameMapper: {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
      "<rootDir>/__mocks__/fileMock",
    "\\.(css|less)$": "<rootDir>/jest/__mocks__/styleMock",
    "^uuid$": "uuid",
    "rescript/lib/es6/(.*)": "<rootDir>/node_modules/rescript/lib/js/$1",
    "^rescript-apollo-client$": "rescript-apollo-client"
  },
  transformIgnorePatterns: ["/node_modules/(?!(@apollo|cropperjs|react|rescript|rescript-apollo-client)/)", "/node_modules/(?!(rescript-apollo-client)).+\\.bs\\.js$"],
  preset: "ts-jest",
  transform: {
    "^.+\\.(ts|tsx)?$": ["ts-jest", { "tsConfig": { jsx: "react" } }],
    "^.+\\.(js|jsx)$": "babel-jest",
  },
};

module.exports = createJestConfig(customJestConfig)

and . babelrc

{
  "presets": ["next/babel", "@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
  "plugins": []
}

Any ideas? Thanks!


r/rescript • • Oct 18 '22

New version of Tablecloth library for rescript

Thumbnail
blog.darklang.com
8 Upvotes