chore(types): Add types to in-page functions like evaluate
(Handle
), waitForFunction
, etc
#8540
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.
What kind of change does this PR introduce?
Improving TypeScript types
Did you add tests for your changes?
Currently: no, but I did update the existing test code
If relevant, did you update the documentation?
Not yet
Summary
Solves
evaluate
,evaluateHandle
, and other related functions not enforcing types.See #8485
Does this PR introduce a breaking change?
Yes.
Page.evaluate
) do not accept strings anymore (except Page.evaluateOnNewDocument).→ Rewrite calls to use regular functions where possible. For dynamic functions, use the
Function()
,AsyncFunction()
orGeneratorFunction()
constructor.→ Just remove them from each call. Types will now be inferred automatically.
JSHandle<T>
are now properly enforced, leading to rejection of improperly typed programs.→ Add type casts or correct code where necessary.
EvalTypes
have been removed.→ I don't expect many people to use these, but use the new alternatives where necessary.
ElementHandle
is nowElementHandle<ParentNode>
instead ofElementHandle<Element>
by default, because it actually always supported things likeDocument
as well.→ Cast to
ElementHandle<Element>
where needed.Other information
Some breaking changes can be reversed (e.g. string overloads or old
EvalTypes
type aliases). However, keeping string overloads will make typing a lot harder and will likely lead to TypeScript rejecting correct programs (at least when I tried it).I did go all out for this one, and you may find I changed some types that shouldn't be / didn't need to be changed, then just tell me.
I intentionally made this a draft PR for now. I'm doing this in my free time because I felt like it so I'm not sure if I'll have time to update docs and such.