Function: defineWithTypes()
Define a command with types
This helper function allows specifying the type parameter of GunshiParams while inferring the Args type, ExtendContext type from the definition.
Since
v0.27.0
Signature
ts
export function defineWithTypes<G extends GunshiParamsConstraint>(): DefineWithTypesReturn<
ExtractExtensions<G>,
ExtractArgs<G>
>Type Parameters
| Name | Description |
|---|---|
G extends GunshiParamsConstraint | A GunshiParams type |
Returns
DefineWithTypesReturn<ExtractExtensions<G>, ExtractArgs<G>> — A function that takes a command definition via define
Examples
ts
// Define a command with specific extensions type
type MyExtensions = { logger: { log: (message: string) => void } }
const command = defineWithTypes<{ extensions: MyExtensions }>()({
name: 'greet',
args: {
name: { type: 'string' }
},
run: ctx => {
// ctx.values is inferred as { name?: string }
// ctx.extensions is MyExtensions
}
})