Skip to content
Cloudflare Docs

TypeScript

The types generated by running wrangler types provide the Service and DurableObjectNamespace types, each of which accepts a single type parameter for the server-side WorkerEntrypoint or DurableObject types. We recommend extending the Env type generated in a separate file and providing the type parameter there, so that these are not overwritten when you run wrangler types again.

For example, if you had a Worker with bindings to a WorkerEntrypoint called SumService and a Durable Object called Counter, your wrangler types output would look like this:

worker-configuration.d.ts
// Generated by Wrangler by running `wrangler types --env-interface BaseEnv`
declare namespace Cloudflare {
interface Env {
SUM_SERVICE: Fetcher;
COUNTER_OBJECT: DurableObjectNamespace;
}
}
interface BaseEnv extends Cloudflare.Env {}

Then extend Env type in a separate file:

env.d.ts
interface Env extends BaseEnv {
SUM_SERVICE: Service<import("../path/to/your/bound/worker").SumService>;
COUNTER_OBJECT: DurableObjectNamespace<import("../path/to/your/do").Counter>;
}

Using higher-order types, we automatically generate client-side stub types (e.g., forcing all methods to be async). In the following example, env.SUM_SERVICE.sum will be typed as defined in the SumService WorkerEntrypoint.

src/index.ts
export default {
async fetch(req, env, ctx): Promise<Response> {
const result = await env.SUM_SERVICE.sum(1, 2);
return new Response(result.toString());
},
} satisfies ExportedHandler<Env>;