Bool vs Js.boolean in ReasonReact

reasonreact

#1

I’ve got this graphql query that is returning and array of type:

{. "id": string, "isPublished": Js.boolean, "text": string, "title": string}

If I render it with this code, it works:

response##feed
                   |> Array.mapi((index, post) =>
                        <div key=(index |> string_of_int)>
                          (post##title |> ste)
                        </div>
                      )
                   |> ReasonReact.arrayToElement

But then if i build a component, <Post and pass post to it, the type changes.

// Post.re
let component = ReasonReact.statelessComponent("Post");

let make = (~post, _children) => {
  ...component,
  render: _self => {
    let title = ! post##isPublished ? post##title ++ "Draft" : post##title;
    <div>
      <Link className="no-underline ma1" href=("/detailpage/" ++ post##id)>
        <h2 className="f3 black-80 fw4 lh-solid"> (title |> Aliases.ste) </h2>
        /* (post##title |> Aliases.ste) */
        <p className="black-80 fw3"> (post##text |> Aliases.ste) </p>
      </Link>
    </div>;
  },
};

and try to return it with:

|> Array.mapi(
      (index, post) =>
      <Post post key=(index |> string_of_int) />
       )
|> ReasonReact.arrayToElement

I get the following type error:


  73 ┆         )
  74 ┆      |> ReasonReact.arrayToElement */  75 ┆   |> Array.mapi((index, post) =>
  76 ┆        <Post post key=(index |> string_of_int) />
  77 ┆      )  78 ┆   |> ReasonReact.arrayToElement
  79 ┆ }

  This has type:
    array(Js.t(({.. id: string, isPublished: bool, text: string,
                  title: string}
                as 'a))) =>
    array(ReasonReact.reactElement)
  But somewhere wanted:
    Js.Array.t({. "id": string, "isPublished": Js.boolean, "text": string,
                 "title": string}) =>
    'b

  The incompatible parts:
    array(Js.t('a))
    vs
    Js.Array.t({. "id": string, "isPublished": Js.boolean, "text": string,
                 "title": string})
      (defined as
      array({. "id": string, "isPublished": Js.boolean, "text": string,
              "title": string}))
    at <anonymous> isPublished are incompatible

Why has the isPublished type changed from Js.boolean to bool in the <Post component? Am i not sending to <Post what was returned from the query?

Thanks for any guidance.


#2

@idkjs The newest release of bucklescript has removed the distinction between bool and Js.boolean. This means that it would be better to use bool in your types going forward.


#3

@ncthbrt im using 3.0. Could this be caused by a dependency that is using 3.0?


#4

Perhaps. Or perhaps it is being caused by a dependency which isn’t using 3?


#5

Right, what i meant obviously. Is there a way to force dependencies to use 3 to see what happens?


#6

Do you have control over this type?
{. "id": string, "isPublished": Js.boolean, "text": string, "title": string}
If so maybe try changing that to use bool?


#7

I tried retyping with

type post = {. "id": string, "isPublished": Js.boolean, "text": string, "title": string};

then

| Data(response) =>
                   let feed: array(post) = response##feed;
                   feed
                   |> Array.mapi((index, post) =>
                        <PostItem post key=(index |> string_of_int) />
                      )
                   |> ReasonReact.arrayToElement;
                 }

didnt work. The repo.

Update: : I installed 3.0 in /node_modules/reason-apollo, same error so I guess that isnt it.


#8

Have you tried cleaning your project first?


#9

i cleaned. then started passing the param directly

| Data(response) =>
                   response##feed
                   |> Array.mapi((index, post) =>
                        <PostItem
                          key=(index |> string_of_int)
                          title=post##title
                          isDraft=(! post##isPublished)
                          post
                        />
                      )
                   |> ReasonReact.arrayToElement

which work for everything except the bool issue which comes back up when I add the isDraft=(! post##isPublished) line.

Unrelated, random question. This is not a use case for nact.io, is it?


#10

Not in its current state. Haven’t really tried getting it to run in the browser. Though it actually may not be that difficult to do. Think mainly need to poly fill setImmediate.


#11

@idkjs I cloned your repo but couldn’t reproduce your compile time error. The build succeeds. I am using yarn btw. Don’t know if that makes a difference.


#12

@ncthbrt that’s nuts. thanks for that. im going to try the same thing and see what happens. brb.


#13

Why don’t you have bs-platform as a dev dependency btw?


#14

So…npm link bs-platform doesnt take care of that?


#15

The advantage of adding bs-platform to dev dependencies is that then you can reliably set the version of the compiler.


#16

That code had the erroring code commented out. I dont know if you uncommented it. This repo, https://github.com/idkjs/bool-vs-jsboolean, will show you the error with yarn and yarn start. This is the line throwing the error noted in readme.md, https://github.com/idkjs/bool-vs-jsboolean/blob/652d57c833806fd48b0aeec6d01f1d8b82c7a46a/src/components/FeedPage.re#L75

I’m still getting it. You didnt happen to uncomment, did you?


#17

I didn’t. Btw I got a yarn warning saying that reason-appol has an incorrect peer depenency on bs-platform@^2.1.0.

Update: After cloning https://github.com/idkjs/bool-vs-jsboolean and then running yarn install, yarn build appears to work correctly on my machine.

Oh I see you left the failing code uncomented. Will see what is the specific problem

Mind running npm install -g bs-platform reason-cli plz


#18

To confirm, this code is giving you issues?

 | Data(response) =>
                   response##feed
                   |> Array.mapi((index, post) =>
                        <PostItem post key=(index |> string_of_int) />
                      )
                   |> ReasonReact.arrayToElement

This is not giving me a compiler error


#19

Wow. I’m super confused right now. I have been using yarn exclusively. Only npm command I ever run is npm link bs-platform.

I ran:

npm -g ls bs-platform reason-cli
/usr/local/lib
├── bs-platform@2.2.1
└── reason-cli@3.1.0-darwin

While in yarn global it was 3.0.0.

Then I ran npm -g bs-platform reason-cli which updated npm and symlinked it to yarn. I have no idea if that affects anything. Or if thats wrong or what.

I opened a new terminal in root of repro repo and got the same error that you are not getting.

Failed to compile.

./src/index.re
Module build failed: Error: We've found a bug for you!
  /Users/prisc_000/Downloads/bool-vs-jsboolean-master/src/components/FeedPage.re 75:23-77:23

  73 ┆         )
  74 ┆      |> ReasonReact.arrayToElement */
  75 ┆   |> Array.mapi((index, post) =>  76 ┆        <PostItem post key=(index |> string_of_int) />
  77 ┆      )
  78 ┆   |> ReasonReact.arrayToElement
  79 ┆ }

  This has type:
    array(Js.t(({.. id: string, isPublished: bool, text: string,
                  title: string}
                as 'a))) =>
    array(ReasonReact.reactElement)
  But somewhere wanted:
    Js.Array.t({. "id": string, "isPublished": Js.boolean, "text": string,
                 "title": string}) =>
    'b

  The incompatible parts:
    array(Js.t('a))
    vs
    Js.Array.t({. "id": string, "isPublished": Js.boolean, "text": string,
                 "title": string})
      (defined as
      array({. "id": string, "isPublished": Js.boolean, "text": string,
              "title": string}))
    at <anonymous> isPublished are incompatible

What would you do next to chase this down?

Restarting machine doesn’t help.


#20

Yes, that is the code that throws the error.