Skip to content
Logo

T.nullish

An absence primitive: the inner schema's value, null, or undefined. It is an ordinary schema you can use anywhere.

T.nullish(T.string.whereby({ length: { max: 10 } })); // string | null | undefined, 1/3 each
T.nullish(inner).weighted({ null: 1, undefined: 1, value: 2 });

T.nullish picks between the three outcomes directly, for the same reason T.optional does. Nesting T.nullable(T.undefinable(inner)) would give 50% null, 25% undefined, and 25% a value, instead of even odds.

For just null or a value, use T.nullable. For just undefined or a value, use T.undefinable.