The First Cry of Atom Today is the first day of the rest of my life.

Using Puppeteer in TypeScript

Finally DefinitelyTyped provides the type definition of Puppeteer.

Puppeteer is a node API for headless Chrome. It is easy to use and provides intuitive API. For example, here is a example in getting started.

import * as puppeteer from 'puppeteer';

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://google.com');
  await page.pdf({path: 'google.pdf'});

  await browser.close();
})();

You would understand what it does even at first look. What we need to do to use Puppeteer in TypeScript are 1) Install Puppeteer and Puppeteer type definitin, 2) Include type definition file into tsconfig.json.

$ npm install --save puppeteer
$ npm install --save-dev @types/puppeteer

Type definition file is put in node_modules/@types/puppeteer/index.d.ts.

{
  "compilerOptions": {
    "module": "commonjs",
    "lib": ["es2015", "dom"],
    // ...
  },
  "include": [
    "src",
    "node_modules/@types/puppeteer/index.d.ts"
  ]
}

You can use Puppetter without any warning now.