Skip to content
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

chore(types): Add types to in-page functions like evaluate(Handle), waitForFunction, etc #8540

Closed
wants to merge 1 commit into from

Conversation

stevenwdv
Copy link

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.

  • Functions accepting page functions (e.g. 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() or GeneratorFunction() constructor.
  • Generic parameters of these functions have changed.
    → Just remove them from each call. Types will now be inferred automatically.
  • Types of these functions and of JSHandle<T> are now properly enforced, leading to rejection of improperly typed programs.
    → Add type casts or correct code where necessary.
  • Some type aliases in EvalTypes have been removed.
    → I don't expect many people to use these, but use the new alternatives where necessary.
  • ElementHandle is now ElementHandle<ParentNode> instead of ElementHandle<Element> by default, because it actually always supported things like Document 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.

…), `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.
@conventional-commit-lint-gcf
Copy link

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@google-cla
Copy link

google-cla bot commented Jun 21, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@stevenwdv stevenwdv changed the title Add types to in-page functions like evaluate(Handle), waitForFunction, etc chore(types): Add types to in-page functions like evaluate(Handle), waitForFunction, etc Jun 21, 2022
@OrKoN OrKoN requested a review from jrandolf June 22, 2022 08:03
@jrandolf
Copy link
Contributor

Closed in favor of #8547.

@jrandolf jrandolf closed this Jun 22, 2022
@stevenwdv
Copy link
Author

stevenwdv commented Jun 22, 2022

@jrandolf Hey I don't wish to be too salty, but you could have at least told me you were working on this, now it feels like I did all of this for nothing.

Anyway, I'm glad the types are there at least. Feel free to take any code from my PR where desired...

@jrandolf
Copy link
Contributor

@stevenwdv I wouldn't cut yourself short! I've also checked out your PR and kept note of some of the ideas used there. The general layout changed drastically though through internal discussions.

@stevenwdv
Copy link
Author

@jrandolf Ah ok I'm glad to hear that :) At first it seemed to me like you didn't look at the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants