mobily/ts-belt

`AR.toOption` for `undefined` promise resolutions resulting in `{ BS_PRIVATE_NESTED_SOME_NONE: 0 }`

Opened this issue · 1 comments

When using the new AR functions, we've run into a scenario when calling AR.toOption on a promise that has resolved undefined will result in the underlying value being { BS_PRIVATE_NESTED_SOME_NONE: 0 } (where O.None would instead be expected).

For example:

const toOptionTest = async () => {
  const option = await pipe(AR.resolve(undefined), AR.toOption);

  console.log('option is:', option);

  O.match(
    option,
    () => {
      console.log('option is `Some`');
    },
    () => {
      console.log('option is `None`');
    },
  );

  return option;
};

void toOptionTest();

In the above, option is inferred by the Typescript compiler to be O.Option<undefined>, however the following is printed:

2023-12-06 09:15:04 option is: { BS_PRIVATE_NESTED_SOME_NONE: 0 }
2023-12-06 09:15:04 option is `Some`

I would instead expect the "option is `None`" log line to be printed, and the someFn branch of O.match not to have been called.


This appears to be an underlying Rescript value which is returned by AR.toOption because Some(value) is called here even if the value is None:

promise->thenResolve(option => {
switch option {
| Ok(value) => Some(value)

Hi. I made a PR to resolve this issue. please have a look and let me know if it's right.