puppeteer/scripts
Jack Franklin ea2b0d1f62
chore: improve type inference of evaluate (#7267)
This commit updates the JSHandle class to take a generic representing
the underlying object that it's wrapping. We can then define
`ElementHandle` as a class that extends `JSHandle<Element>` and begin
to get better type inference.

Prior to this commit the following code would have `d` set to `any`:

```
const div: page.$<HTMLDivElement>('div')
const text = await div.evaluate(d => d.innerText)
```

You could work around this in two ways:

```
const text = await div.evaluate<(d: HTMLDivElement) => string>(d => d.innerText)
const text = await div.evaluate((d: HTMLDivElement) => d.innerText)
```

But both of these have two issues:

1. Requires the user to type extra information.
2. There's no type checking: in the code above I could type `d` as
   `number` and TS would be happy.

With the change here to `evaluate` the user can now type the original
code:

```
const div: page.$<HTMLDivElement>('div')
const text = await div.evaluate(d => d.innerText)
```

And TypeScript will know that `d` is an `HTMLDivElement`.

This change brings us inline with the approach that @types/puppeteer
takes. If we land this and it works, we can do the same with
`evaluateHandle` to hopefully make a similar improvement there.

BREAKING: because this changes the types, which were previously `any`,
this is technically a breaking change as users using TS could start
getting errors after this change is released.
2021-05-26 13:46:17 +00:00
..
ensure-correct-devtools-protocol-package.ts chore: enforce file extensions on imports (#6202) 2020-07-13 10:22:26 +01:00
ensure-pinned-deps.ts chore: enforce pinned dependencies (#7238) 2021-05-14 12:02:36 +01:00
test-install.sh test(install): check for Firefox binary, clean up messages (#6316) 2020-08-10 10:22:31 +02:00
test-ts-definition-files.ts chore: improve type inference of evaluate (#7267) 2021-05-26 13:46:17 +00:00
tsconfig.json chore(agnostic): Remove use of util.promisify (#6446) 2020-09-28 10:35:35 +01:00