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
[Types] Improve types for evaluate
and evaluateHandle
on various types (and JSHandle<T>
)
#8485
Comments
Thanks for helping us improve our types! It seems you've already investigated this. Mind opening a PR? |
@jrandolf nice. First a question: I think for backwards compatibility the Related to that: Should the |
Oh, actually: TypeScript does not support partially specifying generic parameters, so even if we keep the first parameter the same, no other parameters can be added... I'll see if that is even possible here. Edit: actually, it would be possible if the rest of the parameters have default values, but I'm not sure if all this is possible in this case. So it'd still be nice to know if the interface should be retained or not |
At some point, the types will have to be redone due to a separate issue of our types not matching the API docs. Because of this, we can work through this on a PR while we wait for this to happen :) |
@jrandolf Ok! I'm struggling a bit with the types, mostly because TypeScript gets a bit confused with the function and string union, since the evaluate functions can also take a string parameter. Maybe typing this is not impossible, but it requires extra code and really doesn't want to work well. It would massively simplify the typing if the option to pass a string was just removed. It's usecase is covered anyway by just using something like |
Actually, I guess it's necessary for dynamically constructed functions... Not sure how to solve this Edit: Although, you can still use |
…), `waitForFunction`, etc Issue: puppeteer#8485 BREAKING CHANGE: Functions accepting page functions (e.g. `Page.evaluate`) do not accept strings anymore (except Page.evaluateOnNewDocument). Generic parameters of these functions have changed. Types of these functions are now properly enforced, leading to rejection of improperly typed programs. Some type aliases in `EvalTypes` have been removed.
I created a draft PR! #8540 |
@jrandolf I just checked and the issue that e.g. |
I think this was fixed via the branded types? cc @jrandolf |
This doesn't seem to work well with xpath... I was using |
@Maxim-Mazurok Take a look at #8579, they could try to improve the template string types for xpath query handlers. |
Currently the
evaluate
andevaluateHandle
functions andJSHandle<T>
itself are very loosely typed, for example, this is allowed:Problems are:
evaluateHandle
.JSHandle<A> extends JSHandle<B>
for anyA
,B
. Same forElementHandle<E>
.Solution
This could be solved by typing the functions as follows:
With this version, the TypeScript compiler will complain about the
demo
function from earlier.Feel free to copy parts of this code if you would want to.
Note that without the
_dummy
field it is not just the case that we can assign anyJSHandle
to any other, but we also have thatPassed<ElementHandle<HTMLElement>> extends unknown
instead ofHTMLElement
.Wrapper/workaround for current Puppeteer versions (informational)
You can already accomplish this in current versions using the following wrapper functions:
The text was updated successfully, but these errors were encountered: