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

Export all interfaces in declaration file #1682

Merged
merged 1 commit into from
Jan 26, 2021

Conversation

dominik-korsa
Copy link
Contributor

fixes #1540, fixes #1649

Note: I don't know what I'm doing, but this fix seems to be working for my case

In these examples, the code compiles and the console.log output Hello as expected

import cheerio from 'cheerio';

const $: cheerio.Root = cheerio.load('<div>Hello</div>');
console.log($('div').text());
import cheerio, { Root } from 'cheerio';

const $: Root = cheerio.load('<div>Hello</div>');
console.log($('div').text());
import type { Root } from 'cheerio';
import cheerio from 'cheerio';

const $: Root = cheerio.load('<div>Hello</div>');
console.log($('div').text());

Tested with this tsconfig: https://gist.github.com/dominik-korsa/5888e24c196e50fa673154e030cf11ce

I wasn't able to test CommonJS (require()) syntax

@5saviahv
Copy link
Contributor

Good catch, it seems it is not good practice to use same variable name twice 🤔

declare namespace cheerio {...}
...
declare const cheerio: cheerio.CheerioAPI;
export = cheerio;

@rondonjon
Copy link

rondonjon commented Jan 23, 2021

@5saviahv

Good catch, it seems it is not good practice to use same variable name twice

It seems to be quite common, see Advanced Combinations for some examples

@rondonjon
Copy link

rondonjon commented Jan 23, 2021

The proposed solution also solves #1692 for me.

Since your solution is more compatible, I have closed PR #1695.

@keyiis
Copy link

keyiis commented Jan 23, 2021

@dominik-korsa Are you installed @types/cheerio?if use cheerio own types,still not work.

@dominik-korsa
Copy link
Contributor Author

As far as I know @types/cheerio is only for versions 0.x, as versions 1.x will include bundled declarations.

@keyiis
Copy link

keyiis commented Jan 23, 2021

@dominik-korsa i used cheerio version

"cheerio": "^1.0.0-rc.3"

i already solved #1696

@ejose19
Copy link

ejose19 commented Jan 25, 2021

Would be great if this is merged, as exposing interfaces makes it easier to work than ReturnType<typeof cheerio.load> or similar.

@fb55 fb55 merged commit 897b37f into cheeriojs:main Jan 26, 2021
@fb55
Copy link
Member

fb55 commented Jan 26, 2021

Thanks @dominik-korsa!

@CzBiX
Copy link

CzBiX commented Feb 3, 2021

image

This fix doesn't works for me.
Please export all interfaces like this: https://github.com/axios/axios/blob/master/index.d.ts

Also the import cheerio from 'cheerio' doesn't work for ES6, see https://stackoverflow.com/a/56348146

@jpujol880807
Copy link

image

This fix doesn't works for me.
Please export all interfaces like this: https://github.com/axios/axios/blob/master/index.d.ts

Also the import cheerio from 'cheerio' doesn't work for ES6, see https://stackoverflow.com/a/56348146

like @keyiis said downgrade your version of cheerio to "cheerio": "^1.0.0-rc.3" and install @types/cheerio. Those type definitions work with that version but no with the 1.0.0-rc.5 then u can do import cheerio from 'cheerio' and use cheerio.Root and so on
image

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

Successfully merging this pull request may close these issues.

Feature Request: export Root from the types The Cheerio interface is not exported in the type definitions
8 participants