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 schemaconst 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 receiveRecord<string, unknown>. With it, required params arestring/number/booleanand 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.