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

Add named export for more convenient usage for applications using ES modules #56

Open
atsu85 opened this issue May 30, 2023 · 1 comment

Comments

@atsu85
Copy link

atsu85 commented May 30, 2023

Problem

Currently example usage (written for CommonJS modules) doesn't work for ES modules (aka ESM where application has "type": "module" in package.json), because default exports don't play nice with ESM.

This affects both TypeScript and vanilla JS (with ES, not CommonJS modules), but if you'd use TypeScript, you would get a compile-time error when using like in the example from readme:

import to from 'await-to-js'
...
const [err, data] = await to<ServerResponse>(p)
src/main.ts:9:27 - error TS2349: This expression is not callable.
  Type 'typeof import("/home/ats/proj/easypark/productivity/github-patcher/node_modules/await-to-js/dist/types/await-to-js")' has no call signatures.

9 const [err, data] = await to<ServerResponse>(p)
                            ~~

Workaround

To work around it, you would need to replace to( with to.default(, like this:

const [err, data] = await to.default<ServerResponse>(p)

This is nasty - it requires different usage for ESM and CommonJS (can't use the same approach for both module systems).

ESM compatible nicer solution

Instead of exporting only default export (that would keep backwards compatibility for CommonJS), you could also add named export for the same function, so that applications, that use ES modules could use

import { to } from 'await-to-js'
const [err, data] = await to(p)

(that also would work for CommonJS modules)
instead of the default import + workaround mentioned above:

import to from 'await-to-js'
const [err, data] = await to.default(p)
@ghost
Copy link

ghost commented May 30, 2023 via email

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

No branches or pull requests

1 participant