SchemaStaticsAlpha Interface
Stateless APIs exposed via SchemaFactoryBeta as both instance properties and as statics.
For more information about our API support guarantees, see here.
This type is "sealed," meaning that code outside of the library defining it should not implement or extend it. Future versions of this type may add members or make typing of readonly members more specific.
Signature
/** @sealed */
export interface SchemaStaticsAlpha
Properties
| Property | Alerts | Modifiers | Type | Description |
|---|---|---|---|---|
| stagedOptional | Alpha | readonly | <const T extends ImplicitAllowedTypes, const TCustomMetadata = unknown>(t: T, props?: Omit<FieldPropsAlpha<TCustomMetadata>, "defaultProvider" | "stagedOptionalUpgrade">) => FieldSchemaAlpha<FieldKind.Optional, T, TCustomMetadata, FieldPropsAlpha<TCustomMetadata>> | Creates a field schema that is Optional in the view but behaves as Required in the stored schema during the rollout period of an optional-field migration. |
| stagedOptionalRecursive | Alpha | readonly | <const T extends System_Unsafe.ImplicitAllowedTypesUnsafe, const TCustomMetadata = unknown>(t: T, props?: Omit<FieldPropsAlpha<TCustomMetadata>, "defaultProvider" | "stagedOptionalUpgrade">) => FieldSchemaAlphaUnsafe<FieldKind.Optional, T, TCustomMetadata, FieldPropsAlpha<TCustomMetadata>> | stagedOptional except tweaked to work better for recursive types. Use with ValidateRecursiveSchema for improved type safety. |
| withDefault | Alpha | readonly | <Kind extends FieldKind, Types extends ImplicitAllowedTypes, TCustomMetadata = unknown>(fieldSchema: FieldSchema<Kind, Types, TCustomMetadata>, defaultValue: NodeProvider<InsertableTreeFieldFromImplicitField<FieldSchema<Kind, Types>>>) => FieldSchemaAlpha<Kind, Types, TCustomMetadata, FieldPropsAlpha<TCustomMetadata> & { defaultProvider: DefaultProvider; }> | Creates a field schema with a default value. |
| withDefaultRecursive | Alpha | <Kind extends FieldKind, Types extends System_Unsafe.ImplicitAllowedTypesUnsafe, TCustomMetadata = unknown>(fieldSchema: System_Unsafe.FieldSchemaUnsafe<Kind, Types, TCustomMetadata>, defaultValue: Unenforced<NodeProvider<System_Unsafe.InsertableTreeFieldFromImplicitFieldUnsafe<System_Unsafe.FieldSchemaUnsafe<Kind, Types>>>>) => FieldSchemaAlphaUnsafe<Kind, Types, TCustomMetadata, FieldPropsAlpha<TCustomMetadata> & { defaultProvider: DefaultProvider; }> | withDefault except tweaked to work better for recursive types. Use with ValidateRecursiveSchema for improved type safety. |
Property Details
stagedOptional
Creates a field schema that is Optional in the view but behaves as Required in the stored schema during the rollout period of an optional-field migration.
For more information about our API support guarantees, see here.
Signature
readonly stagedOptional: <const T extends ImplicitAllowedTypes, const TCustomMetadata = unknown>(t: T, props?: Omit<FieldPropsAlpha<TCustomMetadata>, "defaultProvider" | "stagedOptionalUpgrade">) => FieldSchemaAlpha<FieldKind.Optional, T, TCustomMetadata, FieldPropsAlpha<TCustomMetadata>>;
Type: <const T extends ImplicitAllowedTypes, const TCustomMetadata = unknown>(t: T, props?: Omit<FieldPropsAlpha<TCustomMetadata>, "defaultProvider" | "stagedOptionalUpgrade">) => FieldSchemaAlpha<FieldKind.Optional, T, TCustomMetadata, FieldPropsAlpha<TCustomMetadata>>
Remarks
Use this to incrementally migrate a required field to optional without a coordinated deployment. The migration path is:
- Start with
sf.required(T)— all clients require the field. 2. Deploysf.stagedOptional(T)— new clients can read documents where the field is present or absent, but the stored schema stays Required (so old clients are unaffected). Setting the field toundefinedis rejected because the stored schema still declares the field as required. 3. Deploysf.optional(T)once all clients support the staged optional field — the stored schema becomes Optional and the field can be cleared.
Analogous to staged for allowed types, but for field optionality.
stagedOptionalRecursive
stagedOptional except tweaked to work better for recursive types. Use with ValidateRecursiveSchema for improved type safety.
For more information about our API support guarantees, see here.
Signature
readonly stagedOptionalRecursive: <const T extends System_Unsafe.ImplicitAllowedTypesUnsafe, const TCustomMetadata = unknown>(t: T, props?: Omit<FieldPropsAlpha<TCustomMetadata>, "defaultProvider" | "stagedOptionalUpgrade">) => FieldSchemaAlphaUnsafe<FieldKind.Optional, T, TCustomMetadata, FieldPropsAlpha<TCustomMetadata>>;
Type: <const T extends System_Unsafe.ImplicitAllowedTypesUnsafe, const TCustomMetadata = unknown>(t: T, props?: Omit<FieldPropsAlpha<TCustomMetadata>, "defaultProvider" | "stagedOptionalUpgrade">) => FieldSchemaAlphaUnsafe<FieldKind.Optional, T, TCustomMetadata, FieldPropsAlpha<TCustomMetadata>>
Remarks
This version of stagedOptional has fewer type constraints to work around TypeScript limitations, see Unenforced. See ValidateRecursiveSchema for additional information about using recursive schema.
withDefault
Creates a field schema with a default value.
For more information about our API support guarantees, see here.
Signature
readonly withDefault: <Kind extends FieldKind, Types extends ImplicitAllowedTypes, TCustomMetadata = unknown>(fieldSchema: FieldSchema<Kind, Types, TCustomMetadata>, defaultValue: NodeProvider<InsertableTreeFieldFromImplicitField<FieldSchema<Kind, Types>>>) => FieldSchemaAlpha<Kind, Types, TCustomMetadata, FieldPropsAlpha<TCustomMetadata> & {
defaultProvider: DefaultProvider;
}>;
Type: <Kind extends FieldKind, Types extends ImplicitAllowedTypes, TCustomMetadata = unknown>(fieldSchema: FieldSchema<Kind, Types, TCustomMetadata>, defaultValue: NodeProvider<InsertableTreeFieldFromImplicitField<FieldSchema<Kind, Types>>>) => FieldSchemaAlpha<Kind, Types, TCustomMetadata, FieldPropsAlpha<TCustomMetadata> & { defaultProvider: DefaultProvider; }>
Remarks
Fields with defaults are optional in constructors, allowing them to be omitted when creating new nodes. This works with both required and optional fields:
- **Required fields with defaults**: The field is always present in the tree, but can be omitted from the constructor. The default value is used when the field is not provided.
- **Optional fields with defaults**: Optional fields already default to
undefined, butwithDefaultlets you specify a different default value.
The default value can be provided in two ways (see NodeProvider):
- **A value**: The value is deep-copied for each use, ensuring independence between instances. Prefer this when the default is a fixed value.
- **A generator function**: A function called each time a default is needed. Use this for dynamic defaults (e.g., timestamps, UUIDs) or when explicit control over value creation is required.
Defaults are evaluated eagerly during node construction.
For recursive schemas, use withDefaultRecursive instead.
See the Default Field Values documentation for a comprehensive guide with additional examples.
Example
A schema with a mix of required, defaulted, and dynamic fields:
const factory = new SchemaFactoryAlpha("example");
class Task extends factory.objectAlpha("Task", {
// No default — must always be provided in the constructor
title: factory.required(factory.string),
// Required field with a static default
status: factory.withDefault(factory.required(factory.string), "todo"),
// Optional field with a custom default (instead of `undefined`)
priority: factory.withDefault(factory.optional(factory.number), 0),
// Dynamic default using a generator function
createdAt: factory.withDefault(factory.required(factory.number), () => Date.now()),
}) {}
// Only `title` is required in the constructor; the rest use their defaults
const task = new Task({ title: "Write docs" });
// task.status === "todo"
// task.priority === 0
// task.createdAt is set to the current timestamp
// Defaults can be overridden by providing explicit values
const urgentTask = new Task({ title: "Fix bug", status: "in-progress", priority: 1 });
withDefaultRecursive
withDefault except tweaked to work better for recursive types. Use with ValidateRecursiveSchema for improved type safety.
For more information about our API support guarantees, see here.
Signature
withDefaultRecursive: <Kind extends FieldKind, Types extends System_Unsafe.ImplicitAllowedTypesUnsafe, TCustomMetadata = unknown>(fieldSchema: System_Unsafe.FieldSchemaUnsafe<Kind, Types, TCustomMetadata>, defaultValue: Unenforced<NodeProvider<System_Unsafe.InsertableTreeFieldFromImplicitFieldUnsafe<System_Unsafe.FieldSchemaUnsafe<Kind, Types>>>>) => FieldSchemaAlphaUnsafe<Kind, Types, TCustomMetadata, FieldPropsAlpha<TCustomMetadata> & {
defaultProvider: DefaultProvider;
}>;
Type: <Kind extends FieldKind, Types extends System_Unsafe.ImplicitAllowedTypesUnsafe, TCustomMetadata = unknown>(fieldSchema: System_Unsafe.FieldSchemaUnsafe<Kind, Types, TCustomMetadata>, defaultValue: Unenforced<NodeProvider<System_Unsafe.InsertableTreeFieldFromImplicitFieldUnsafe<System_Unsafe.FieldSchemaUnsafe<Kind, Types>>>>) => FieldSchemaAlphaUnsafe<Kind, Types, TCustomMetadata, FieldPropsAlpha<TCustomMetadata> & { defaultProvider: DefaultProvider; }>
Remarks
This version of withDefault has fewer type constraints to work around TypeScript limitations, see Unenforced. See ValidateRecursiveSchema for additional information about using recursive schema.
See the Default Field Values — Recursive Types documentation for usage examples and guidance on avoiding infinite recursion with recursive defaults.
See Also
SchemaStatics for why this is useful.