<S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<
infer A
>
? NonEmptyArray<A>
: S extends Iterable<infer A>
? Array<A>
: neverReverses an iterable into a new array.
When to use
Use to reverse an iterable into a new array without mutating the original input.
Details
Preserves NonEmptyArray in the return type.
Example (Reversing an array)
import { Array } from "effect"
console.log(Array.reverse([1, 2, 3, 4])) // [4, 3, 2, 1]export const const reverse: <S extends Iterable<any>>(
self: S
) => S extends NonEmptyReadonlyArray<infer A>
? NonEmptyArray<A>
: S extends Iterable<infer A>
? Array<A>
: never
Reverses an iterable into a new array.
When to use
Use to reverse an iterable into a new array without mutating the original
input.
Details
Preserves NonEmptyArray in the return type.
Example (Reversing an array)
import { Array } from "effect"
console.log(Array.reverse([1, 2, 3, 4])) // [4, 3, 2, 1]
reverse = <function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>>(
self: S extends Iterable<any>self: function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS
): function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS extends type NonEmptyReadonlyArray<A> = readonly [
A,
...A[]
]
A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<infer function (type parameter) AA> ? type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) AA> : function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS extends interface Iterable<T, TReturn = any, TNext = any>Iterable<infer function (type parameter) AA> ? interface Array<T>Array<function (type parameter) AA> : never =>
const Array: ArrayConstructorExposes the global array constructor.
When to use
Use to access native JavaScript array constructor methods such as isArray
or from from the Effect module namespace.
Example (Accessing the Array constructor)
import { Array } from "effect"
const arr = new Array.Array(3)
console.log(arr) // [undefined, undefined, undefined]
Array.ArrayConstructor.from<any>(iterable: Iterable<any> | ArrayLike<any>): any[] (+3 overloads)Creates an array from an iterable object.
from(self: S extends Iterable<any>self).Array<any>.reverse(): any[]Reverses the elements in an array in place.
This method mutates the array and returns a reference to the same array.
reverse() as any