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

cache:
  CACHE: default

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: D1Database;
		TASKS: QueueBinding;
		FLOW: WorkflowBinding;
		CACHE: CacheBinding;
		STATE: StateBinding;
		PLOY_AUTH: PloyAuth;
		AI_URL: string;
		AI_TOKEN: string;
	}
}

declare global {
	interface PloyEnv {
		vars: {
			API_URL: string;
		};
		DB: D1Database;
		TASKS: QueueBinding;
		FLOW: WorkflowBinding;
		CACHE: CacheBinding;
		STATE: StateBinding;
		PLOY_AUTH: PloyAuth;
		AI_URL: string;
		AI_TOKEN: string;
	}
}

export {};

Supported Binding Types

ploy.yaml keyType in PloyEnv
envvars: { KEY: string } per variable
dbD1Database per binding name
queueQueueBinding per binding name
workflowWorkflowBinding per binding name
cacheCacheBinding per binding name
stateStateBinding per binding name
fsFileStorageBinding per binding name
timerTimerBinding per binding name
authPloyAuth as PLOY_AUTH
ai: trueAI_URL: string and AI_TOKEN: string properties

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: Request, env: PloyEnv): Promise<Response> {
		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