fix(types): enable type errors w. $x() empty array #7915
Closed
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.
First of all, thanks for all of your continued effort on Puppeteer, really amazing project!
What kind of change does this PR introduce?
Bugfix - if
page.$x
returns an empty array, throw type error when runtime error possible.For example, the runtime error below would also become a type error:
It would require code to guard against
undefined
:Did you add tests for your changes?
No, but I can update tests if necessary
If relevant, did you update the documentation?
I'm hoping the documentation is auto-generated from the TS types, but if necessary I can update the docs manually.
Summary
Motivation is described above.
Does this PR introduce a breaking change?
Yes, this is a breaking change for the types.
Other information
Alternatives considered
A workaround that we've explored for now (although not as nice as the destructuring in the original post above):
The reason why the destructuring + simple guard at the top of this post doesn't work for us is because of
@typescript-eslint/no-unnecessary-condition
, which recognizes this as an unnecessary condition:This is because TypeScript will return the type of
ElementHandle<Element>
for the first destructured element, which will always be truthy.