Is it good idea having binding to JS this?

I’m writing rescript-vitest. I’ve realized that I don’t write a binding for expect(...).toBeInstanceOf method.

I used instanceof check sometimes in ReScript projects, like when I was writing encoder, I did:

switch object->Js.Types.classify {
| JSObject(object) if %raw(`object instanceof Uint8Array`) => { ... }
}

But this time I need it for a random constructor.

I just want to make it clear that toBeInstanceOf is not needed in most ReScript projects. However, it can be useful in self-testing of some JS interop or binding projects.

ex)
expect(myReScriptEncoder(...)).toBeInstanceOf(???);

And I think the most correct way to get a constructor in scope is to refer to this.

@val external this: {..} = "this" // or
@val external globalThis: {..} = "globalThis"

expect(myReScriptEncoder(...)).toBeInstanceOf(Js.globalThis["Uint8Array"]);

If folks agree this can be useful, I will make a PR to add this to the Js Module.

1 Like