The optional schema checks that the type of the value is either the given type, or undefined.
It is the equivalent of the TypeScript question mark (?).
constmaybeString = optional(string); if (is(maybeString)(undefined)) { console.log("maybe a string"); } if (is(maybeString)("hello")) { console.log("maybe a string"); } if (!is(maybeString)(null)) { console.log("not maybe a string"); }
The
optional
schema checks that the type of the value is either the given type, orundefined
. It is the equivalent of the TypeScript question mark (?
).Example