Function

createField

@ng-forge/dynamic-forms

Creates a typed field configuration with helpful error messages for common mistakes.

This helper function provides early validation and clear error messages for common configuration pitfalls that would otherwise cause runtime errors.

Signature

function createField<
  T extends AvailableFieldTypes
>(
  type: T,
  config: Omit<ExtractField<T>, "type">
): ExtractField<T>

Type Parameters

NameConstraintDefaultDescription
TAvailableFieldTypes--

Parameters

NameTypeDescription
typeT- The field type (e.g., 'input', 'select', 'group')
configOmit<ExtractField<T>, "type">- The field configuration (excluding the 'type' property)

Returns

ExtractField<T>

Examples

// Basic usage
const nameField = createField('input', {
  key: 'name',
  label: 'Name',
  value: ''
});

// With validation
const emailField = createField('input', {
  key: 'email',
  label: 'Email',
  required: true,
  email: true,
  props: { type: 'email' }
});

// Select with options (at field level)
const countryField = createField('select', {
  key: 'country',
  label: 'Country',
  options: [{ label: 'USA', value: 'us' }]
});

// Slider with minValue/maxValue (at field level)
const ratingField = createField('slider', {
  key: 'rating',
  label: 'Rating',
  minValue: 1,
  maxValue: 10,
  step: 1,
  value: 5
});