Definitelytyped: [@types/puppeteer] x.evaluate allows out of scope variables

Created on 9 Apr 2020  路  1Comment  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the @types/xxxx package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • @marvinhagemeister

    • @cdeutsch

    • @ksm2

    • @SimonSchick

    • @SerbanGhita

    • @JasonKaz

    • @davecardwell

    • @angrykoala

When using the evaluate function, code is run in the browser's context and not node's. The below code compiles and throws ReferenceError: someText is not defined. If I do not add someText to the arguments of page.evaluate, the compiler should throw an error.

import puppeteer from "puppeteer";

async function main() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    let someText = "Some value";
    await page.evaluate(() => {
        console.log(someText);
    });
}

main();

Versions:

  • tsc: 3.8.3
  • @types/node: "^13.11.1"
  • @types/puppeteer: "^2.0.1"
  • puppeteer: "^2.1.1"

Most helpful comment

That is not something the typings can fix, there is no mechanism that can prevent functions from capturing locals.
iirc they basically call toString() on the function to serialize it, there is nothing we can do.

>All comments

That is not something the typings can fix, there is no mechanism that can prevent functions from capturing locals.
iirc they basically call toString() on the function to serialize it, there is nothing we can do.

Was this page helpful?
0 / 5 - 0 ratings