Array
A value can conform to an array IFF:
- its
typeof
is"object"
Array.isArray
istrue
- all values in the array conform to the provided types
Array.length
is greater than or equal tominLength
(if provided)Array.length
is less than or equal tomaxLength
(if provided)
Usage
import { T } from "type-guarder";
T.Array(T.string()).conforms("Hello, world!"); // false
T.Array(T.string()).conforms(["Hello, world!"]); // true
T.Array(T.string()).conforms(["Hello, world!", "abc"]); // true
T.Array(T.string()).conforms(["Hello, world!", false]); // false
T.Array(T.string(), T.Boolean()).conforms(["Hello, world!", false]); // true
Options
import { T } from "type-guarder";
T.ArrayWithOptions({
types: [T.Number(), T.Boolean()],
options: { minLength: 1 },
}).conforms([1, true]); // true
T.ArrayWithOptions({
types: [T.Number(), T.Boolean()],
options: { minLength: 10 },
}).conforms([1, true]); // false