lucacasonato/deno-puppeteer

Types and execution context (evaluation functions): suggested patterns

Opened this issue · 0 comments

When writing evaluation functions to be executed in a puppeteer page (e.g. page.evaluate(fn)), it is desirable to have access to the native TS window and DOM types within those functions.

The two environments (Deno and browser) share quite a bit of API overlap, but also have some exclusive parts (e.g. globalThis.Deno in Deno vs window.document in browser, etc.). One simple approach is to include all type libs in a Deno config, like this:

deno.json:

{
  "compilerOptions": {
    "lib": [
      "deno.window",
      "dom",
      "dom.iterable"
    ]
  }
}

However — because runtime errors will occur when using browser-exclusive APIs in Deno (or Deno-exclusive APIs in browser evaluation functions) — the above approach prevents the compiler from catching these environment-mismatch errors and emitting related diagnostics.

To me, at least, it is desirable to maintain type safety in these independent execution contexts (Deno vs browser). Are there any established patterns for this?