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 case | Why |
|---|---|
| Creation form | You don't have an existing object to infer from |
| Type-first schema | You want the schema to be statically typed against a specific interface |
| Explicit defaults | You need to define default values explicitly per field |
InferType.tsts
| 1 | import { field } from '@runilib/react-formbridge' |
| 2 | |
| 3 | type User = { |
| 4 | name: string |
| 5 | email: string |
| 6 | age: number |
| 7 | active: boolean |
| 8 | } |
| 9 | |
| 10 | // Schema is typed as Record<keyof User, FieldDescriptor> |
| 11 | const 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:
| Method | Type | Description |
|---|---|---|
type? | FieldType | Force a specific field type - overrides auto-detection |
label? | string | Override the generated label |
placeholder? | string | Override the generated placeholder |
hint? | string | Helper text |
required? | boolean | string | Marks the field required (message optional) |
min? | number | Minimum length/value |
max? | number | Maximum length/value |
options? | SelectOption[] | string[] | Option list for select/radio fields |
disabled? | boolean | Disable the field |
hidden? | boolean | Hide the field |
validate? | (value, allValues) => string | null | Custom inline validator |
If defaultValue is omitted, a sensible default is derived from the field type:
| Field type | Auto-derived default |
|---|---|
number | 0 |
checkbox / switch | false |
| Everything else | '' (empty string) |
InferTypeDefaults.tsts
| 1 | import { field } from '@runilib/react-formbridge' |
| 2 | |
| 3 | type Settings = { |
| 4 | theme: string |
| 5 | fontSize: number |
| 6 | notifications: boolean |
| 7 | } |
| 8 | |
| 9 | const 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) | |
|---|---|---|
| Input | A runtime object with values | A type + field config map |
| Default values | Taken from the object values | Explicit defaultValue or auto-derived |
| Type detection | Automatic from keys & values | You specify type manually |
| Best for | Edit forms, pre-filled data | Creation forms, type-first schemas |
| Typing | Inferred from the object shape | Explicit generic <T> |