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

Fix types #1759

Merged
merged 4 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ declare namespace cheerio {

eq(index: number): Cheerio;

get(): any[];
get(index: number): any;
get(): Element[];
get(index: number): Element | undefined;

index(): number;
index(selector: string): number;
Expand Down
4 changes: 2 additions & 2 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cheerio(html);
cheerio('ul', html);
cheerio('li', 'ul', html);

const $fromElement = cheerio.load($('ul').get(0));
const $fromElement = cheerio.load($('ul').toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe idea here was test does "cheerio.load" accepts type "Element" as input, but I also did see error with last commit, so maybe cast it instead:

const $fromElement = cheerio.load($('ul').get(0) as Element);

I know type "Element" is not defined, but we could import it

import type { Element } from 'domhandler';

Copy link
Contributor Author

@karlhorky karlhorky Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I thought at first too, but I tested the code, and it turns out that code is actually broken, even in JavaScript:

const $fromElement = cheerio.load($('ul').get(0));

It throws an error. cheerio.load() doesn't accept an Element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I tested it and tsd didn't complain. I also tested load function with and it worked, like expected.

const cheerio = require('cheerio');
const html = 
  '<ul id="fruits">\n<li class="orange">Apple</li>\n<li class="class">Orange</li>\n<li class="pear">Pear</li>\n<input type="text" />\n</ul>';

const $ = cheerio.load(html);
const $fromElement = cheerio.load($('li').get(0));

console.log(cheerio.html($fromElement._root));

I copied what you did and added casting cb6f40 and it works. Interesting why it fails on you.

Copy link
Contributor Author

@karlhorky karlhorky Feb 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not talking about tsd / TypeScript though - did you try actually running that as JavaScript? cheerio.load() with an element throws an error - at least it did in my testing.

The type for the load method also seems to indicate that it does not accept an element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry I think I was looking at the wrong overload. It does seem to accept an element here:

load(element: Element | Element[], options?: CheerioParserOptions): Root;

Copy link
Contributor Author

@karlhorky karlhorky Feb 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the cast now, if you would prefer that over the .html() method.

Not sure why it errored for me, but if you think it works, then we can go ahead like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to admit I rarely use typescript, I do see why it is useful but still. I know there was issue with element (still are, rc.5 in npm) #1591, but it should be fixed in repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, yeah, that must have been the error that was thrown.

Copy link
Contributor Author

@karlhorky karlhorky Feb 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And as for the value of TypeScript, it's actually telling us a useful bit of information here:

If the element returned from .get(0) is undefined, then there will be an error with cheerio.load().

So actually, a more robust version of the code (avoiding casting) would be this:

const $ul = $('ul').get(0);

if ($ul) {
  const $fromElement = cheerio.load($ul);

  if ($fromElement('ul > li').length !== 3) {
    throw new Error(
      'Expecting 3 elements when passing `cheerio.Element` to `load()`'
    );
  }
}

But since these are just tests, it doesn't matter too much if there is a cast here.


if ($fromElement('ul > li').length !== 3) {
throw new Error(
Expand Down Expand Up @@ -248,7 +248,7 @@ $el.eq(0).text();
$el.eq(-1).text();

// .get( [i] )
$el.get(0).tagName;
$el.get(0)?.tagName;
$el.get().length;

// .index()
Expand Down