Search code examples
typescriptplaywright

TypeScript cannot find name of window property in Playwright evaluate callback


I am using Typescript with Playwright. I have this code to evaluate JavaScript in the browser:

const CSRFToken = await this.page.evaluate(() => {
  return ACC.config.CSRFToken;
});

But I get an error: Cannot find name 'ACC'.ts(2304).

ACC.config.CSRFToken is something that exists on my page in the script tag of the DOM.

How can I resolve this?


Solution

  • You can declare a variable ACC in your typescript file and use that variable like following.

    declare const ACC: any;
    const CSRFToken = await this.page.evaluate(() => {
        return ACC.config.CSRFToken;
    });