Skip to content

TypeScript

Pages Functions supports TypeScript. Author any files in your /functions directory with a .ts extension instead of a .js extension to start using TypeScript.

To add the runtime types to your project, run:

Terminal window
npm install --save-dev typescript @cloudflare/workers-types

Then configure the runtime types by creating a functions/tsconfig.json file:

{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"types": ["@cloudflare/workers-types"]
}
}

If you already have a tsconfig.json at the root of your project, you may wish to explicitly exclude the /functions directory to avoid conflicts. To exclude the /functions directory:

{
"include": ["src/**/*"],
"exclude": ["functions/**/*"],
"compilerOptions": {}
}

Pages Functions can be typed using the PagesFunction type. This type accepts an Env parameter. To use the env parameter:

interface Env {
KV: KVNamespace;
}
export const onRequest: PagesFunction<Env> = async (context) => {
const value = await context.env.KV.get("example");
return new Response(value);
};