sindresorhus/is

Typescript incompatibilities when using this library in a browser-only project

lo1tuma opened this issue · 3 comments

Hi and thanks for this library it is already really helpful for me.

Unfortunately I’ve stumbled upon an issue for a browser-only project which uses setTimeout() and stored the result of this function call in a local variable of type number. Here is references the nodejs types and thereby includes all its global types. So typescript thinks setTimeout is referring to the nodejs typings, where the return value is not a number instead it is a value of type Timer.

It seems like there is a work around available to use window.setTimeout explicitly or global.setTimeout when using the node timers. But ultimately I would prefer to not have all the global type declaration from node slipped in just because I use is.

Unfortunately I don’t have a good idea how this could be solved. There are a couple of issues on the typescript issue tracker, but nothing really concrete.

I don't know if this library has the same issue, but I had to reference the Buffer type without including the entirety of @types/node, and I ended up just adding the following in a *.d.ts file:

declare module 'buffer' {
  global {
    interface Buffer extends Uint8Array {}
  }
}

It works for me because @types/node is not included by default and if the user includes @types/node then that interface gets merged with the one in Node automatically so it just works. The only minor issue is that now there's a global "Buffer" interface, which isn't ideal but there's no way around it I think, and that interface doesn't really extend Uint8Array anyway so as soon as the user tries to use any Buffer-only methods they'll get an error somewhere I guess.

I'm happy to use the declare module 'buffer' workaround.