Core API

defineCommand

Type-safe command definitions with full TypeScript inference

defineCommand#

defineCommand is a helper that gives you fully inferred TypeScript types in your handler based on the param schema you declare โ€” with zero runtime overhead (identity function).

typescript
import { createSurf, defineCommand } from '@surfjs/core'
ย 
// params are automatically typed from the schema
const search = defineCommand({
description: 'Search products',
params: {
query: { type: 'string', required: true, description: 'Search term' },
limit: { type: 'number', description: 'Max results' },
},
run(params) {
// params.query โ†’ string (required โ€” never undefined)
// params.limit โ†’ number | undefined (optional)
return db.products.search(params.query, { limit: params.limit })
},
})
ย 
const surf = await createSurf({
name: 'My Store',
commands: { search },
})

๐Ÿ’ก Tip: Without defineCommand, handlers receive Record<string, unknown>. With it, required params are string / number / boolean and optional params include | undefined โ€” all inferred automatically.

Use defineCommand when you want type-safe handlers without writing manual interfaces. It's especially useful for large APIs with many commands.