facebook/idx

idx swallows some null-related errors that optional chaining do not

Closed this issue · 2 comments

Here is a serious incompatibility with optional-chaining.

Testcase:

var a = { b: null };
a?.b.c; // should throw a TypeError, as `a.b` is null

With idx, as currently implemented in https://github.com/facebookincubator/idx/blob/master/packages/idx/src/idx.js#L58

var a = { b: null };
idx(a, _ => _.b.c); // do not throw error

In order to follow more closely the spec, idx should not catch every null-related error, but test the nullity of one specific value, e.g.:

const idx = (input, accessor)  => input == null ? input : accessor(input);

Of course, this would be a serious BC breaking change for idx.

On the other side, if the intent of idx(a, _ => _.b.c) is to implement a?.b?.c (rather than a?.b.c as I assumed), the current implementation is ok-ish.

I’m closing this issue, since, by reading more closely the README, the intent is indeed not to emulate a?.b.c, but a?.b?.c.