slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.31k stars 278 forks source link

xregexp and typescript - how to use it? #269

Closed tdjastrzebski closed 4 years ago

tdjastrzebski commented 5 years ago

How do I correctly use xregexp in typescript? In a new console nodejs app I did: npm init npm install xregexp --save-dev On the line where I try to use XRegExp received error reads: TypeError: xregexp_1.default is not a function I tried to install type definition as well: npm install @types/xregexp --save-dev but this did not help. I think there is version mismatch anyway - latest types is version 3.0.30 (vs xregexp@4.2.4). I does not seem to matter how I import XRegExp (see #218). I tried: import * as xregexp from 'xregexp'; import { XRegExp } from 'xregexp'; but the error is always the same. What am I missing?

tdjastrzebski commented 5 years ago

Answering own question:

  1. set esModuleInterop = true in tsconfig.json
  2. use import XRegExp from 'xregexp';
  3. do not install @types/xregexp - as of August 2019 there is no current version available

My suggestion: as typescript becomes more and more popular it may make a sense to maintain current type definitions and release together with the javascript package.

codingedgar commented 4 years ago

I'm currently doing

// @ts-ignore
import XRegExp from "xregexp";

to avoid the types errors , would be nice to contrib to the types.

pristinesource commented 4 years ago

Answering own question:

  1. set esModuleInterop = true in tsconfig.json
  2. use import XRegExp from 'xregexp';
  3. do not install @types/xregexp - as of August 2019 there is no current version available

My suggestion: as typescript becomes more and more popular it may make a sense to maintain current type definitions and release together with the javascript package.

@tdjastrzebski There is a pull request for an update to @types/xregexp. Once it is merged it will provide definitions for XRegExp v4.3.0. https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43115

wyattjoh commented 4 years ago

Looks like the mentioned PR has been merged!