Skip to main content

Writing Type-safe apps

Keywork uses TypeScript to ensure type-safety while you develop your web app. And with built-in error guards, Keywork allows your app to fail gracefully if a runtime exception occurs while your app is deployed.

Runtime Configuration

Cloudflare Workers

Type definitions are automatically included when installing Keywork via NPM. However, if you're using Skypack

Cloudflare also provides type definition for their runtime via @cloudflare/workers-types

yarn add --dev @cloudflare/workers-types

Using environment bindings with TypeScript

It's recommended that you create an ambient type file for any environment bindings your Worker uses.

types/bindings.d.ts
export {}

declare global {
const MY_ENV_VAR: string
const MY_SECRET: string
const myKVNamespace: KVNamespace
}

Deno Deploy

Keywork can be used directly via Deno Land:

worker
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
import { RequestRouter } from 'https://deno.land/x/keywork/modules/router/mod.ts'

const app = new RequestRouter()

app.get('/', () => <h1>Hello from Keywork! 👋</h1>)

serve((request) => app.fetch(request))

Import Maps

Import Maps are recommended to avoid long import URLs:

importmap.json
{
"imports": {
"keywork/router": "https://deno.land/x/keywork/router/mod.ts",
"keywork/uri": "https://deno.land/x/keywork/uri/mod.ts"
// etc
}
}