Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS compat: enable default importing #25

Closed
wants to merge 2 commits into from

Conversation

d-fischer
Copy link

This enables TypeScript developers that don't use esModuleInterop to consume this library when compiling for both the commonjs and es2015 module systems from the same source code.

In the current state, there are two options:

If I use

import * as escape from 'escape-string-regexp';
escape('foo');

it compiles fine in both cases, but throws an error like escape is not a function in the es2015 build at runtime. The commonjs build runs fine.

Now if I use

import escape from 'escape-string-regexp';
escape('foo');

instead, it throws a compile error, and if you ignore it, there's an error like escape_string_regexp_1.default is not a function at runtime in the commonjs build. The es2015 build runs fine.

The latter is the easier thing to fix, and it's what I fixed in this PR. In essence, I'm changing the "correct" way (for the TS compiler) from the former to the latter, and making it run in either module environment.

It's also a breaking change for TS users without esModuleInterop, though.

@d-fischer
Copy link
Author

Huh. These tests now pass for me locally, but they still fail on Travis.

@sindresorhus
Copy link
Owner

The TypeScript teams says we shouldn't do this. See: sindresorhus/memoize#31

@d-fischer
Copy link
Author

That other issue was closed because it makes a point about this "just being for intellisense". Now, in a world of node being able to handle ESM without a transpiler as well (and TS producing ES modules as output too), this isn't true anymore. It is entirely impossible to use this package in a way that is compatible with both ESM and CJS output, if you have esModuleInterop off, without having two entirely separate code bases. As I showed, there is no syntax that works on runtime (!) with both output formats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants