-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Improve typescript accuracy #5168
Comments
Take a look at |
@connorjclark I amended the fix for the |
We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days. |
We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it! |
The intended way to use puppeteer + typescript today is by installing
@types/puppeteer
. However, there are some gaps in these types:CDPSession
is ignorant of the protocol commands / events and return types. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9f57df89f30bacee1d46dfbf40a08132b4b059ef/types/puppeteer/index.d.ts#L2151Page.emulate(emulateOptions)
- types claim that bothviewport
anduserAgent
are optional, but actually both are required https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9f57df89f30bacee1d46dfbf40a08132b4b059ef/types/puppeteer/index.d.ts#L519These discrepancies can be avoided if the type definitions are generated from the source directly.
Approach 1: Generate declaration files from JS
This seemed like the ideal approach - generate a
.d.ts
for every module and publish on npm. Users could use the types like this:Ultimately, this didn't work, I think due to bugs within TS. Here's what would need to be done.
Upgrade to TS 3.7
3.7 added support of
--declaration
with--allowJs
. microsoft/TypeScript#7546 (comment)tsc errors for
events
This pattern trips up TS when generated the declaration file:
The error manifests like this:
Using
const EventEmitter = require('events').EventEmitter
fixes this. Yeah, that works - it was originally the only way to importEventEmitter
, and has stuck around. TS doesn't error b/c@types/node
references a non-exported name for the default export, but for this named export it doesn't.But, all modules that import a puppeteer class that extends
EventEmitter
still get the same error:So, this is a blocker.
Bad generated declaration files
USKeyboardLayout.js
generates this.d.ts
file:Which isn't even valid:
Another blocker.
:(
At this point, I gave up.
Approach 2: Convert to TypeScript
This would certainly make great types available for puppeteer, but would be a lot of work.
Approach 3: Just fixup
@types/puppeteer
At the very least, I'd like to fix the lack of good CDP types in
@types/puppeteer
. Puppeteer internally defines these inlib/externals.d.ts
. But it uses aprotocol.d.ts
that is generated based on the packaged Chromium version, so it makes sense to just import that file into@types/puppeteer
:This requires publishing
protocol.d.ts
to npm, and tweaking the script that generates that file toexport default Protocol;
so it can be imported.With these changes, the following would be well-typed:
Some docs on how to use TypeScript would be good. Basically:
@types/puppeteer
The text was updated successfully, but these errors were encountered: