mobily/ts-belt

O.from* returns value or undefined, not Some or None

Closed this issue ยท 1 comments

Hey @mobily,
Thanks so much for this library, I love it! I'm having a small problem with O.Option though which might be a genuine issue or more likely I'm expecting the wrong thing.

Steps to Reproduce

You can run this from the Terminal to quickly see it

mkdir temp
cd temp
npm init -y
npm install @mobily/ts-belt@3.13.1
node -e "
  const { O } = require('@mobily/ts-belt')
  console.log(O.fromFalsy(1))
  console.log(O.fromFalsy('hello world'))
  console.log(O.fromFalsy([]))
  console.log(O.fromFalsy(0))
  console.log(O.fromFalsy(''))
  console.log(O.fromNullable(1))
  console.log(O.fromNullable('hello world'))
  console.log(O.fromNullable([]))
  console.log(O.fromNullable(false))
  console.log(O.fromNullable(null))
  console.log(O.fromNullable(undefined))
"

Expected Output

I'm expecting Options back...

Some(1)
Some('hello world')
Some([])
None
None
Some(1)
Some('hello world')
Some([])
Some(false)
None
None

Actual Output

...but get literal values for Some and undefined for None.

1
hello world
[]
undefined
undefined
1
hello world
[]
false
undefined
undefined

Other Comments

The same seems to happen with O.fromPredicate and O.fromExecution.

When doing the same thing with R.Result though, I get what I would expect.

const { R } = require("@mobily/ts-belt");
R.fromFalsy(new Error("..."))(1);
// { TAG: 0, _0: 1 }
R.fromFalsy(new Error("..."))(0);
// {
//   TAG: 1,
//   _0: Error:
// }

Thanks for your help ๐Ÿ‘

mobily commented

hello @JamieMason ๐Ÿ‘‹ I'm glad you like ts-belt!

but get literal values for Some and undefined for None.

this is how specifically the ReScript compiler works, please see a quick example: https://rescript-lang.org/try?version=v10.1.2&code=DYUwLgBAdg9lIDEoQLwQBQEpUD4IDk4QBuAKFEgGcYBbRZNLXCAZVpHQEZMzzwIAHqmhEkWMhQgBPYdTpiepIA

also you can find more details in the ReScript docs: https://rescript-lang.org/docs/manual/latest/null-undefined-option#interoperate-with-javascript-undefined-and-null