Skip to content
Logo

T.optional

An absence primitive: a field that might be missing, present-as-undefined, or present with a value. Only meaningful inside T.object(...).

T.optional(inner); // ~33% omitted, ~33% undefined, ~33% value
T.optional(inner).weighted({ omitted: 1, undefined: 1, value: 2 });

Uniform at 1/3 each by default. Reweight any of the three named outcomes; weights are relative proportions. A weight of 0 disables that outcome ({ omitted: 0 } is always present — as undefined or as a value). Every weight zero throws; negative weights, NaN, and Infinity are invalid.

Zeroing changes the distribution, not the type: T.optional(x).weighted({ omitted: 0 }) still types the key as optional. A narrower kind (T.undefinable, a required field) is still how you change the type.

Force the field off (e.g. in .override() or .fabricate(overrides)), use the exported Omitted sentinel:

import { Omitted } from "@ghostry/fabricator";
 
WidgetFabricator.fabricate({ nickname: Omitted });

For omitted-or-present (no undefined value), use T.omittable. For a key that's always present with a possibly-undefined value, use T.undefinable.