mathiasbynens / emoji-regex

A regular expression to match all Emoji-only symbols as per the Unicode Standard.
https://mths.be/emoji-regex
MIT License
1.72k stars 175 forks source link

Typescript error when using require('emoji-regex') #102

Open rabarrota opened 1 year ago

rabarrota commented 1 year ago

I'm getting the following Intellisense error when using require('emoji-regex')

This expression is not callable.
  Type 'typeof import("emoji-regex")' has no call signatures.ts(2349)

Some context for how I'm using this:

const emojiRegex = require('emoji-regex');

...

const sanitizedText = text.replace(emojiRegex(), '');

This package works fine this way when actually being used. It just always shows this Typescript error.

Screen Shot 2022-10-07 at 11 15 46
ghost commented 1 year ago

Interesting, I can't repro this, it works fine for me:

image

Do you think it's maybe because you're using require() instead of import?

Maybe because the type declaration module uses declare module { } it only works if you're using module-style imports?

Also what version of the package are you running?

rabarrota commented 1 year ago

Yup, I'm assuming that's the problem. I'm using cjs so it doesn't look like the type declaration supports it.

I'm using 10.2.1

vdh commented 8 months ago

The index.d.ts should be updated to this instead:

diff --git a/index.d.ts b/index.d.ts
index 6f57555090390e09a3b3a2e39bab037c995b47b9..beb73be29022bf20734057e38148282207f5fa2b 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,3 +1,4 @@
 declare module 'emoji-regex' {
-  export default function emojiRegex(): RegExp;
+  function emojiRegex(): RegExp;
+  export = emojiRegex;
 }

It looks like it was changed by #89 for the index.mjs file but that then broke types for the index.js file. With split files like this, it should probably be defined directly inside those files as JSDoc instead if they're going to vary in typing like that.