Is it possible to tell TypeScript a particular function call in a Node module should use browser's types and not Node's?

Viewed 71

In Playwright code inside page.evaluate runs in browser environment, while code outside it runs in Node. Since TypeScript doesn't understand this (unsurprisingly), it thinks setTimeout has type

declare function setTimeout(callback: (...args: any[]) => void, ms?: number, ...args: any[]): NodeJS.Timeout;

from node_modules/@types/node/globals.d.ts. But inside page.evaluate setTimeout returns a number. Is there a nice way to tell TS about it? For now I just cast the result

setTimeout(...) as unknown as number

but I would prefer to avoid type assertions if possible.

0 Answers
Related