rescript-lang.org icon indicating copy to clipboard operation
rescript-lang.org copied to clipboard

Add the "{..}" construct to syntax lookup

Open alarbada opened this issue 2 years ago • 6 comments

I have no idea what that actually does, I found it in some declaration files.

alarbada avatar Feb 24 '23 11:02 alarbada

That is an open object type. They are mostly relevant for external usage where you'd receive a object of "any structure" and you'd then use it the way you think it should be used.

Example:

@val external someObj: {..} = "someObj"

someObj["name"] = "Patrick"
someObj["twitter"] = "@ryyppy"

Playground Link

ryyppy avatar May 26 '23 07:05 ryyppy

Mmm, so like an object map, kind of a { [ key: string]: any } in typescript?

Shouldn't then this be on the syntax lookup?

Edit: I mean, these quirks are really difficult to search. I am partly not using rescript because of that (also I don't believe I'm the target audience).

alarbada avatar May 26 '23 09:05 alarbada

There's a few more subtle things to consider when using this type. (had to create a new issue to clarify some things here).

It should be on the syntax lookup and most likely in the object / interop sections at some point.

ryyppy avatar May 26 '23 09:05 ryyppy

Edit: I mean, these quirks are really difficult to search. I am partly not using rescript because of that (also I don't believe I'm the target audience).

If it helps. This is used very rarely, it has legitimate uses, but is generally an escape hatch.

The { [ key: string]: any } type is closer to Js.Dict.t<any>, only there is no any type in ReScript. Js.Json.t is often used for any unknown data.

CarlOlson avatar May 26 '23 12:05 CarlOlson

Hi @ryyppy and @CarlOlson, Actually, this escape hatch is used in a very important place (where it shouldn't be). I started a thread here and an issue here. TL;DR:

<input
  onChange={evt => {
    let x = ReactEvent.Form.target(evt)["value"] // x is 'a
    setIntegerValue(_ => x) // x is int, should not happen!
  }}
/>

I agree with @alarbada, this looks like something that shouldn't even take place in a language like Rescript.

@ryyppy I think in my example the compiler should panic if there's no decoder in place because evt.target.value can be anything and will blow up at runtime. Would you agree that a decoder should be used here?

ivan-demchenko avatar Dec 01 '23 11:12 ivan-demchenko