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
| Flag | Description | Default |
|---|---|---|
-o, --output <path> | Output file path | env.d.ts |
Usage
Run ploy types at the root of your project after any change to ploy.yaml:
ploy types
# Generated env.d.tsThe generated file is automatically referenced in tsconfig.json. Commit it to your repository.
Custom Output Path
ploy types -o src/env.d.tsGenerated Output
Given a ploy.yaml like:
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: trueploy types generates:
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 key | Type in PloyEnv |
|---|---|
env | vars: { KEY: string } per variable |
db | Database per binding name |
queue | QueueBinding per binding name |
workflow | WorkflowBinding per binding name |
state | StateBinding per binding name (KVNamespace compatible) |
fs | FileStorageBinding per binding name |
timer | TimerBinding per binding name |
auth | PloyAuth as PLOY_AUTH |
ai: true | AI: Ai, PLOY_AI_URL: string, and PLOY_AI_TOKEN: string |
agentSDK: true | All 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:
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:
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:
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
{
"scripts": {
"types": "ploy types",
"dev": "ploy dev",
"build": "ploy build",
"postinstall": "ploy types"
}
}How is this guide?
Last updated on