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

fix: type page event listeners correctly #6891

Merged
merged 1 commit into from Mar 25, 2021
Merged

Conversation

jackfranklin
Copy link
Collaborator

This PR fixes the fact that currently if you have:

page.on('request', request => {

})

Then request will be typed as any. We can fix this by defining an
interface of event name => callback argument type, and looking that up
when you call page.on.

Fixes #6854.

/**
* Optional response headers. All values are converted to strings.
*/
headers: Record<string, any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this to any?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, not sure. I think we stringify them anyway so unknown is better here.

requestfailed: HTTPRequest;
requestfinished: HTTPRequest;
workercreated: WebWorker;
workerdestroyed: WebWorker;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding a [event: string]: any at the bottom here to make sure we don't break places that use other event names? Or is that intended?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather it fail explicitly because we're missing a type.

This PR fixes the fact that currently if you have:

```ts
page.on('request', request => {

})
```

Then `request` will be typed as `any`. We can fix this by defining an
interface of event name => callback argument type, and looking that up
when you call `page.on`.

Also includes a drive-by fix to ensure we convert response headers to
strings, and updates the types accordingly.
@jackfranklin jackfranklin merged commit 866d34e into main Mar 25, 2021
@jackfranklin jackfranklin deleted the page-event-object branch March 25, 2021 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

puppeteer.HTTPRequest resourceType() returns string instead of a resource type enum
3 participants