Why do i need to import Fetcher from @cloudflare/workers-types

import puppeteer from "@cloudflare/puppeteer";
import { Fetcher } from "@cloudflare/workers-types";

interface Env {
MYBROWSER: Fetcher;
}

export default {
async fetch(request: Request, env: Env): Promise<Response> {
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto("https://example.com");
const metrics = await page.metrics();
await browser.close();
return Response.json(metrics);
},
};
import puppeteer from "@cloudflare/puppeteer";
import { Fetcher } from "@cloudflare/workers-types";

interface Env {
MYBROWSER: Fetcher;
}

export default {
async fetch(request: Request, env: Env): Promise<Response> {
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.goto("https://example.com");
const metrics = await page.metrics();
await browser.close();
return Response.json(metrics);
},
};
` heello. in code above i have to import Fetcher type. in cloudflare docs they don't import types. and also when i bind Fetcher type to MYBROWSER i can not use it in puppeteer.launch(env.MYBROWSER). i get this error that says argument Fetcher is not assignable to parameter of type 'BrowserWorker'
5 Replies
Hello, I’m Allie!
If you place it in your tsconfig.json like this:
{
"compilerOptions": {
"types": ["@cloudflare/workers-types"],
// ...
}
}
{
"compilerOptions": {
"types": ["@cloudflare/workers-types"],
// ...
}
}
, then it should be available without having to import it
Avocadio
AvocadioOP16mo ago
no sir it did not work and without typescript, i can not use it to launch new browser browser = { binding = "MY_BROWSER", type = "browser" } i bind it too
Hello, I’m Allie!
Try reloading your editor. I'm getting clean types with this:
Avocadio
AvocadioOP16mo ago
no sir i still get type errors nah i can not get it i think there are problems with puppeteer's last version what do you guys think? still can not work it
Niek
Niek14mo ago
with the tsconfig that @HardlySpookin’ pasted above it works fine. i would recommend to have create-cloudflare create the tsconfig too
Want results from more Discord servers?
Add your server