Ploy
Ploy
CLI

ploy types

Generate TypeScript type definitions from your ploy.yaml bindings.

ploy types

Generate TypeScript type definitions from your ploy.yaml bindings. Creates an env.d.ts file and updates tsconfig.json so that your editor and compiler know about all the runtime bindings available to your worker or Next.js app.

ploy types [options]

Options

FlagDescriptionDefault
-o, --output <path>Output file pathenv.d.ts

Usage

Run ploy types at the root of your project after any change to ploy.yaml:

ploy types
# Generated env.d.ts

The generated file is automatically referenced in tsconfig.json. Commit it to your repository.

Custom Output Path

ploy types -o src/env.d.ts

Generated Output

Given a ploy.yaml like:

ploy.yaml
kind: worker

env:
  API_URL: https://api.example.com

db:
  DB: default

queue:
  TASKS: tasks

workflow:
  FLOW: my_flow

state:
  STATE: default

auth:
  binding: PLOY_AUTH
ai: true

ploy types generates:

env.d.ts
declare module "@meetploy/nextjs" {
	interface PloyEnv {
		vars: {
			API_URL: string;
		};
		DB: Database;
		TASKS: QueueBinding;
		FLOW: WorkflowBinding;
		STATE: StateBinding;
		PLOY_AUTH: PloyAuth;
		AI: Ai;
		PLOY_AI_URL: string;
		PLOY_AI_TOKEN: string;
	}
}

declare global {
	interface PloyEnv {
		vars: {
			API_URL: string;
		};
		DB: Database;
		TASKS: QueueBinding;
		FLOW: WorkflowBinding;
		STATE: StateBinding;
		PLOY_AUTH: PloyAuth;
		AI: Ai;
		PLOY_AI_URL: string;
		PLOY_AI_TOKEN: string;
	}
}

export {};

With ai: true, the generated types expose env.AI.run() first, and also include PLOY_AI_URL / PLOY_AI_TOKEN for direct gateway calls when needed.

Supported Binding Types

ploy.yaml keyType in PloyEnv
envvars: { KEY: string } per variable
dbDatabase per binding name
queueQueueBinding per binding name
workflowWorkflowBinding per binding name
stateStateBinding per binding name (KVNamespace compatible)
fsFileStorageBinding per binding name
timerTimerBinding per binding name
authPloyAuth as PLOY_AUTH
ai: trueAI: Ai, PLOY_AI_URL: string, and PLOY_AI_TOKEN: string
agentSDK: trueAll Agent SDK bindings (ai, state, fs, workflow, timer)

StateBinding keeps Ploy's existing binding names while supporting the Cloudflare KV methods get, put, delete, getWithMetadata, and list.

When @meetploy/agent-sdk is installed in your project and agentSDK: true is not set in ploy.yaml, running ploy types will automatically add it for you.

Using the Types

After running ploy types, the PloyEnv global interface is available everywhere in your project. With @meetploy/start:

src/index.ts
import { ploy } from "@meetploy/start";

const worker = ploy<PloyEnv>()
	.get("/", {}, async (ctx) => {
		// ctx.env is fully typed
		const users = await ctx.env.DB.prepare("SELECT * FROM users").all();
		return { users: users.results };
	})
	.build();

export default worker;

With a plain worker:

src/index.ts
export default {
	async fetch(request, env) {
		const result = await env.DB.prepare("SELECT * FROM users").all();
		return Response.json(result.results);
	},
};

With Next.js via @meetploy/nextjs:

app/api/route.ts
import { getPloyEnv } from "@meetploy/nextjs";

export async function GET() {
	const env = getPloyEnv<PloyEnv>();
	const result = await env.DB.prepare("SELECT * FROM users").all();
	return Response.json(result.results);
}

Run ploy types any time you add or remove bindings in ploy.yaml. Add it to your postinstall or prepare script to keep types in sync automatically.

package.json Integration

package.json
{
	"scripts": {
		"types": "ploy types",
		"dev": "ploy dev",
		"build": "ploy build",
		"postinstall": "ploy types"
	}
}

How is this guide?

Last updated on