Skip to content

gunshi / definition / defineWithTypes

Function: defineWithTypes()

ts
function defineWithTypes<G>(): DefineWithTypesReturn<ExtractExtensions<G>, ExtractArgs<G>>;

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.

Type Parameters

Type ParameterDescription
G extends GunshiParamsConstraintA GunshiParams type

Returns

DefineWithTypesReturn<ExtractExtensions<G>, ExtractArgs<G>>

A function that takes a command definition via define

Example

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
  }
})

Since

v0.27.0

Released under the MIT License.