-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix types #1759
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work
types/index.test-d.ts
Outdated
@@ -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()); |
There was a problem hiding this comment.
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';
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
Line 282 in 1c2c0c0
load(element: Element | Element[], options?: CheerioParserOptions): Root; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Thanks a lot @karlhorky! |
Glad to help :) Thanks for the merge! |
Hi there, first of all, thanks so much for Cheerio! Cool to be able to load an HTML string in anywhere and traverse so easily.
Shouldn't
.get()
always return eitherElement[]
, anElement
orundefined
?