Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1813

Binding to union types in record fields in JS/TS

$
0
0

Broadly speaking, I’d go with option 1. However, one important thing to remember is this: you don’t have to bind to all available values.

For example, per gesture’s docs, passing a Delay of true is equivalent to setting it to 180; this gives you a little flexibility in how you represent it. (I’m guessing setting it to false is the same as not passing it at all.)

One alternative would be to just make the delay an int, optionally providing the default value of 180 as a constant instead.

type config = {delay: int}
// If you want to provide it
let defaultDelay = 180

If you’d still like modules, could also do:

module Delay = {
  type t = int
  let default = 180
}
type config = {delay: Delay.t}

You can even see this in the official bindings to React hooks: useState only takes the callback overload, rather than a direct constant. This would also be a possible simplification in your case: it looks like a few options take either a callback or a constant. You’d only really need to bind to the function case.


Viewing all articles
Browse latest Browse all 1813

Trending Articles