react-formbridge
Browse documentation
Field buildersv1.0.2

field.inferType()

field.inferType<T>(fields) generates a schema purely from a TypeScript type - no object instance needed. You describe each property with its configuration, and the schema is fully typed against T.

The same helper is also exported as inferFromType<T>(fields) when you prefer a direct utility import.

This is useful when:

Use caseWhy
Creation formYou don't have an existing object to infer from
Type-first schemaYou want the schema to be statically typed against a specific interface
Explicit defaultsYou need to define default values explicitly per field
InferType.tsts
1import { field } from '@runilib/react-formbridge'
2
3type User = {
4 name: string
5 email: string
6 age: number
7 active: boolean
8}
9
10// Schema is typed as Record<keyof User, FieldDescriptor>
11const schema = field.inferType<User>({
12 name: { label: 'Full name', required: true, min: 2 },
13 email: { label: 'Email', required: true },
14 age: { label: 'Age', min: 18, defaultValue: 18 },
15 active: { label: 'Active', type: 'switch' },
16})

Default values

Each field entry accepts an optional defaultValue. The rest of the field entry surface is the same InferFieldOptions contract used by field.infer().

Complete InferFieldOptions surface:

MethodTypeDescription
type?FieldTypeForce a specific field type - overrides auto-detection
label?stringOverride the generated label
placeholder?stringOverride the generated placeholder
hint?stringHelper text
required?boolean | stringMarks the field required (message optional)
min?numberMinimum length/value
max?numberMaximum length/value
options?SelectOption[] | string[]Option list for select/radio fields
disabled?booleanDisable the field
hidden?booleanHide the field
validate?(value, allValues) => string | nullCustom inline validator

If defaultValue is omitted, a sensible default is derived from the field type:

Field typeAuto-derived default
number0
checkbox / switchfalse
Everything else'' (empty string)
InferTypeDefaults.tsts
1import { field } from '@runilib/react-formbridge'
2
3type Settings = {
4 theme: string
5 fontSize: number
6 notifications: boolean
7}
8
9const schema = field.inferType<Settings>({
10 theme: { label: 'Theme', type: 'select',
11 options: ['light', 'dark', 'auto'], defaultValue: 'auto' },
12 fontSize: { label: 'Font size', min: 10, max: 32, defaultValue: 16 },
13 notifications: { label: 'Enable notifications', type: 'switch' },
14 // notifications defaults to false (switch type)
15})

field.infer() vs field.inferType()

field.infer(obj)field.inferType<T>(fields)
InputA runtime object with valuesA type + field config map
Default valuesTaken from the object valuesExplicit defaultValue or auto-derived
Type detectionAutomatic from keys & valuesYou specify type manually
Best forEdit forms, pre-filled dataCreation forms, type-first schemas
TypingInferred from the object shapeExplicit generic <T>