Skip to main content

optional

function

Combinator that makes any SerializationSchema optional. The returned schema yields `undefined` when the value is `undefined` (so `undefined` is its recoverable default) and otherwise delegates to `inner`. Use it for serialized properties that may be absent and, when absent, should stay absent (an exported `T | undefined` property is omitted from the JSON rather than persisted). Pass `{omitDefault: true}` when an in-band value equal to `inner`'s default means "absent" rather than "explicitly this value" — the historical `serializedNode.width || undefined` idiom, where a falsy `0` is not a real width. Such a value (and any out-of-domain input, which `inner` coerces to its default) yields `undefined`, so it is omitted from the exported JSON instead of being persisted as the default. Equality is `inner`'s own (see SerializationSchema.isEqual), so a reference-typed default is compared by content: `optional(arrayValue(...), {omitDefault: true})` omits an explicitly empty array rather than persisting it.

function optional<T, In = T>(inner: SerializationSchema<T, never, In>, options?: { readonly omitDefault?: boolean; }): SerializationSchema<T | undefined, never, In | undefined>

Import

import { optional } from '@modular/sdk/editor';

Notes

example
```ts const parseWidth = optional(numberValue()); // ^? SerializationSchema<number | undefined> parseWidth(120); // 120 parseWidth(undefined); // undefined (the recoverable default) const parseCellWidth = optional(numberValue(), {omitDefault: true}); parseCellWidth(0); // undefined (0 is not a real width) parseCellWidth('x'); // undefined (coerced to the default, then omitted) ```
__NO_SIDE_EFFECTS__