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

feat(types): expose typedefs to consumers #6745

Merged
merged 1 commit into from Jan 13, 2021
Merged

Conversation

jackfranklin
Copy link
Collaborator

This PR replaces #6289 and
instead puts all TS typedefs into one folder, lib/typings, which we then
can point TS to via the types field in package.json.

The previous PR (#6289) tried
to merge all our typedefs into one big file via API Extractor, but this
isn't really necessary. We can instead put all the individual .d.ts
files into one folder, and TS will still understand them and provide a
good experience to the developer.

One important consideration is that you could consider this a breaking
change. I'm not sure how likely it is, but it could cause problems for:

  • Projects that didn't have any type info for Puppeteer and treated all
    its exports as any may now start having legitimate type failures.
  • Projects that depend on the @types/puppeteer package may have issues
    if they now swap to use this one and the types aren't quite aligned.

In addition, once we do ship a release with this change in, it will mean
that we have to treat any changes to any type definitions as
release-note-worthy, and any breaking changes to type definitions will
need to be treated as breaking code changes (nearly always a breaking
type def means a breaking change anyway), but it's worth considering
that once we expose these typedefs we should treat them as first class
citizens of the project just like we would with the actual source code.

This PR replaces #6289 with a
simpler approach to types where we compile them all alongside the
compiled code (so for every `foo.js` that is generated, we generate
`foo.d.ts`).

By default that's not enough, as when you `import puppeteer from
'puppeteer'` in Node land you import `cjs-entry.js`, so we also create
`cjs-entry.d.ts` which TypeScript will then pick up. This type file
tells TypeScript that the thing that `cjs-entry.js` exposes is an
instance of the `Puppeteer` class, which then hooks all the types up.

The previous PR (#6289) tried
to merge all our typedefs into one big file via API Extractor, but this
isn't really necessary.
good experience to the developer.

One important consideration is that you _could_ consider this a breaking
change. I'm not sure how likely it is, but it could cause problems for:

* Projects that didn't have any type info for Puppeteer and treated all
  its exports as `any` may now start having legitimate type failures.
* Projects that depend on the `@types/puppeteer` package may have issues
  if they now swap to use this one and the types aren't quite aligned.

In addition, once we do ship a release with this change in, it will mean
that we have to treat any changes to any type definitions as
release-note-worthy, and any breaking changes to type definitions will
need to be treated as breaking code changes (nearly always a breaking
type def means a breaking change anyway), but it's worth considering
that once we expose these typedefs we should treat them as first class
citizens of the project just like we would with the actual source code.

I also fully expect to have some bugs with our types, or have users
create issues/PRs to change our types, but I think that's a good thing
and it should settle down.

I've tested this locally by creating a new package, linking Puppeteer
via `npm link puppeteer` and then checking that VSCode correctly shows
me the right things when I use `Go to Definition` on something that
comes from the Puppeteer source code.
@jackfranklin
Copy link
Collaborator Author

tsconfig-esm.json was removed as it's not the TSConfig we actually use and must have been mistakenly left behind by a previous PR, so I'm removing it now.

@mathiasbynens mathiasbynens merged commit ebd087a into main Jan 13, 2021
@mathiasbynens mathiasbynens deleted the ship-type-defs branch January 13, 2021 09:37
@@ -0,0 +1,3 @@
declare const _default: typeof import('./lib/cjs/puppeteer/node').default;

export default _default;
Copy link
Contributor

Choose a reason for hiding this comment

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

a cjs entry should have an export = definition.

@AviVahl
Copy link
Contributor

AviVahl commented Jan 15, 2021

also, ideally compatibility with the existing @types/puppeteer should be verified prior to release.
otherwise this is going to break quite a lot of (typescript-based) projects.
see #5233 for an example of how this can be actually tested.

@bodinsamuel
Copy link
Contributor

For those wondering how to load (like me) the current d.ts until it's released:

tsconfig.json
{
   "paths": {
      "puppeteer-core": [
        "node_modules/puppeteer-core/lib/esm/puppeteer/node"
      ]
    }
 }

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.

None yet

4 participants