Why there is no safe array findIndex methods?

I was looking for something tho find an index that gives me an option, but to my surprise there is none.
Why is this? I can easily add a wrapper, but this makes the API really inconsistent. I want all the array method that may not find an answer to have a coherent interface. Why some random methods perform mutations others just return negative indexes etc?

The Js module (and @rescript/core) act mainly as “zero cost bindings” which means they’re meant to compile down 1:1 to the JS equivalent without adding any extra code. Since the JS function returns an int, the binding needs to return an int. This helps keep bundle size down.

For the method to return an option you’d have to add extra code to inspect the return type. There’s the Belt library that provides some additional tools specifically for cases like yours, and it comes with Belt.Array.getIndexBy to get it as an option

1 Like

You might find this discussion interesting: [Discussion] Immutable defaults for mutable JS API:s · Issue #23 · rescript-association/rescript-core · GitHub.

It gets at what you’re mentioning with tradeoffs between consistency and close-to-JS bindings.