-
-
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
Merged
Merged
Fix types #1759
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
I know type "Element" is not defined, but we could import it
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:
It throws an error.
cheerio.load()
doesn't accept anElement
.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.
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:
cheerio/types/index.d.ts
Line 282 in 1c2c0c0
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)
isundefined
, then there will be an error withcheerio.load()
.So actually, a more robust version of the code (avoiding casting) would be this:
But since these are just tests, it doesn't matter too much if there is a cast here.