defineConfig() once with a plain object, use keyring() to mark sensitive fields, and use optional() to mark fields that may be absent. TypeScript infers all value types from the schema automatically — no manual type annotations required.
defineConfig(schema)
Validates a schema object at runtime and returns it unchanged. The return value is used for TypeScript inference when constructing a Configurate instance.
Signature:
defineConfig() checks that every keyring() ID in the schema is unique. It throws a runtime error if any IDs are duplicated.
A plain object where each key maps to a schema value:
String, Number, Boolean, a keyring() field, an optional() field, a nested object, or a single-element array.S — the same schema object, unchanged.
keyring(typeCtor, opts)
Marks a schema field as keyring-protected. At runtime, the value is stored in the OS keyring (Windows Credential Manager, macOS Keychain, or Linux Secret Service) instead of being written to disk. When loaded without unlocking, the field appears as null.
Signature:
The value type constructor:
String, Number, or Boolean.A unique identifier for this keyring field within the schema. Used as part of the OS keyring user string (
{account}/{id}). Must not be empty and must not contain /.KeyringField<T, Id>
optional(schema)
Marks a schema field as optional. An optional field may be absent from the stored config object. Validation (validateOnRead / validateOnWrite) does not fail when the field is missing. The inferred TypeScript type includes undefined.
Signature:
The schema value to make optional. Can wrap primitives, keyring fields, nested objects, or arrays.
OptionalField<V>
Schema value types
| Type | TypeScript type | Description |
|---|---|---|
String | string | Required string field |
Number | number | Required number field (must be finite) |
Boolean | boolean | Required boolean field |
keyring(Type, { id }) | string | number | boolean | OS keyring-protected field; appears as null when locked |
optional(Type) | T | undefined | Optional field; may be absent from the config object |
{ ... } | object | Nested object with its own schema keys |
[Type] | Type[] | Array of a single element type |
Type utilities: InferLocked and InferUnlocked
These generic utility types derive the full TypeScript type of your config data from a schema.
InferUnlocked<S>
Infers the config type with keyring fields as their actual runtime types. Use this type when you have unlocked data (returned from .unlock(keyringOpts)).
InferLocked<S>
Infers the config type with keyring fields replaced by null. Use this type when you have locked data (returned from .run() without unlocking).
Complete schema example
src/lib/schema.ts