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

TypeScript bindings don't work as expected #324

Closed
CodingDoug opened this issue Jun 18, 2020 · 1 comment · Fixed by #326
Closed

TypeScript bindings don't work as expected #324

CodingDoug opened this issue Jun 18, 2020 · 1 comment · Fixed by #326
Assignees

Comments

@CodingDoug
Copy link

CodingDoug commented Jun 18, 2020

When importing a module using TS, it's expected to be able to write a line like this:

import * as posthtml from 'posthtml'
posthtml(...)

This would get the exports from the module into a single identifier. However, since the default export is a function, it actually doesn't work at all. You get the following error message:

TypeError: posthtml_1.default is not a function

There is no easy workaround for this, given the current type bindings. It's generally suggested to avoid using default exports.

express handles this pretty well. You can see its TS bindings here.

A similar fix for posthtml would involve making its binding look more like this:

declare namespace posthtml { ... }

declare function posthtml<TThis, TMessage>(
  plugins?: posthtml.Plugin<TThis>[]
): posthtml.PostHTML<TThis, TMessage>;

export = posthtml

There might be a better way, but this worked for me. I am able to use my example import code from above this way. TS is very important for my project, and having proper bindings will go a long way.

@mrmlnc
Copy link
Member

mrmlnc commented Jun 21, 2020

Hello, @CodingDoug,

Thanks for the detailed information for the issue.

You're right. The current typings are written incorrectly — we must use export = xxx.

@mrmlnc mrmlnc linked a pull request Jun 21, 2020 that will close this issue
6 tasks
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 a pull request may close this issue.

2 participants