node-util icon indicating copy to clipboard operation
node-util copied to clipboard

Add `types`

Open SimenB opened this issue 5 years ago • 3 comments

https://nodejs.org/api/util.html#util_util_types

They work cross-realm (at least for Error coming from a new vm.Context). Not sure if it's worth it for you to work cross-realm, but just doing instanceof should work OK as a shim, think?

SimenB avatar Oct 27 '20 15:10 SimenB

It should be 100% doable in most cases, i have tons of npm packages to handle most of them in a cross-realm way. https://npmjs.com/~inspect-js

ljharb avatar Oct 27 '20 16:10 ljharb

Oh, nice! I'm specifically after a an error check as we gave a bunch of ugly duck typing in Jest to figure out if we have an error instance or not. But I'm hesitant to add utils usage in case it makes it harder to use parts of Jets in the browsers.

Do you have a package for Error (i.e. util.types.isNativeError)? Quick searching in the link you posted didn't find it, sorry if I missed something obvious. If so I'd love to use that and not worry about if it is shimmed by this package if people attempt to use e.g. expect or something in the browser

SimenB avatar Oct 27 '20 16:10 SimenB

No, unfortunately there is no JavaScript way to robustly identify a native Error - instanceof Error is the best you can do.

ljharb avatar Oct 27 '20 19:10 ljharb