requireArray()function
requireArray(arr: MutableArray<T>, min: 1, max: 1, caller?: AnyCaller): [T]
requireArray(arr: MutableArray<T>, min: 2, max: 2, caller?: AnyCaller): [T, T]
requireArray(arr: MutableArray<T>, min: 3, max: 3, caller?: AnyCaller): [T, T, T]
requireArray(arr: MutableArray<T>, min?: 1, max?: number, caller?: AnyCaller): [T, ...T[]]
requireArray(arr: MutableArray<T>, min: 2, max?: number, caller?: AnyCaller): [T, T, ...T[]]
requireArray(arr: MutableArray<T>, min: 3, max?: number, caller?: AnyCaller): [T, T, T, ...T[]]
requireArray(arr: MutableArray<T>, min?: number, max?: number, caller?: AnyCaller): MutableArray<T>
requireArray(arr: ImmutableArray<T>, min: 1, max: 1, caller?: AnyCaller): readonly [T]
requireArray(arr: ImmutableArray<T>, min: 2, max: 2, caller?: AnyCaller): readonly [T, T]
requireArray(arr: ImmutableArray<T>, min: 3, max: 3, caller?: AnyCaller): readonly [T, T, T]
requireArray(arr: ImmutableArray<T>, min?: 1, max?: number, caller?: AnyCaller): readonly [T, ...T[]]
requireArray(arr: ImmutableArray<T>, min: 2, max?: number, caller?: AnyCaller): readonly [T, T, ...T[]]
requireArray(arr: ImmutableArray<T>, min: 3, max?: number, caller?: AnyCaller): readonly [T, T, T, ...T[]]
requireArray(list: PossibleArray<T>, min?: number, max?: number, caller?: AnyCaller): ImmutableArray<T>
| Param | Type | |
|---|---|---|
arr | MutableArray<T> | Mutable array: an array that can be changed. required |
min | 1 | Minimum number of items the array must contain (defaults to 0). required |
max | 1 | Maximum number of items the array may contain (defaults to Infinity). required |
caller | AnyCaller | Function to attribute a thrown error to (defaults to requireArray itself). |
min | 2 | required |
max | 2 | required |
caller | AnyCaller | Any calling function or constructor, usually referring to something that can call in the current scope that can appear in a stack trace. |
min | 3 | required |
max | 3 | required |
min | 1 | |
max | number | |
min | number | |
arr | ImmutableArray<T> | Immutable array: an array that cannot be changed. required |
min | 1 | required |
max | 1 | required |
list | PossibleArray<T> | Things that can be converted to arrays. required |
| Return | |
|---|---|
[T] | An array of the items within the length bounds. |
[T, T] | |
[T, T, T] | |
[T, ...T[]] | |
[T, T, ...T[]] | |
[T, T, T, ...T[]] | |
MutableArray<T> | Mutable array: an array that can be changed. |
readonly [T] | |
readonly [T, T] | |
readonly [T, T, T] | |
readonly [T, ...T[]] | |
readonly [T, T, ...T[]] | |
readonly [T, T, T, ...T[]] | |
ImmutableArray<T> | Immutable array: an array that cannot be changed. |
| Throws | |
|---|---|
RequiredError | If list cannot be converted to an array within the length bounds. |
Convert a possible array to an array (optionally with specified min/max length), or throw RequiredError if conversion fails.
Examples
requireArray(new Set([1, 2])); // [1, 2]
requireArray([], 1); // throws RequiredError