Skip to content
Logo

T.undefinable

An absence primitive: either the inner schema's value or undefined. It is an ordinary schema you can use anywhere: a top-level Fabricator, an array element, or an object field that is always present.

T.undefinable(T.string.whereby({ length: { max: 10 } })); // string | undefined, 50/50
T.undefinable(inner).weighted({ undefined: 1, value: 9 }); // undefined 10% of the time

A weight of 0 turns that outcome off, so { undefined: 0 } always gives the inner value. It throws if both weights are 0. Negative weights, NaN, and Infinity are invalid. A weight of 0 changes the odds, not the type: the value is still typed T | undefined.

This decides the value, not whether a key exists. For a field that is sometimes left off the object, use T.omittable. For a field that can be left out, undefined, or a value, use T.optional.