maybe
Create a strictly typed TypeObject that also accepts null
and undefined
values.
type.maybe(Type)
Given a type, returns a TypeObject that will check values against against that type. Throws if the value is not of the provided type or null
or undefined
.
import { Reflect, type } from "can/everything";
let val = Reflect.convert(42, type.maybe(Number));
console.log(val); // -> 42
val = Reflect.convert(null, type.maybe(Number));
console.log(val); // -> null
val = Reflect.convert(undefined, type.maybe(Number));
console.log(val); // -> undefined
Reflect.convert("42", type.maybe(Number));
// throws for providing an invalid type.
Parameters
- Type
{function}
:A constructor function that values will be checked against.