anyUtility types for working with ReadonlyArray at the type level. Use these
to infer element types, preserve non-emptiness, and flatten nested arrays.
export declare namespace ReadonlyArray {
/**
* Infers the element type of an iterable.
*
* **Example** (Inferring an element type)
*
* ```ts
* import type { Array } from "effect"
*
* type StringArrayType = Array.ReadonlyArray.Infer<ReadonlyArray<string>>
* // StringArrayType is string
* ```
*
* @category types
* @since 2.0.0
*/
export type type ReadonlyArray.Infer<S extends Iterable<any>> = S extends readonly (infer A)[] ? A : S extends Iterable<infer A> ? A : neverInfers the element type of an iterable.
Example (Inferring an element type)
import type { Array } from "effect"
type StringArrayType = Array.ReadonlyArray.Infer<ReadonlyArray<string>>
// StringArrayType is string
Infer<function (type parameter) S in type ReadonlyArray.Infer<S extends Iterable<any>>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>> = function (type parameter) S in type ReadonlyArray.Infer<S extends Iterable<any>>S extends interface ReadonlyArray<T>ReadonlyArray<infer function (type parameter) AA> ? function (type parameter) AA
: function (type parameter) S in type ReadonlyArray.Infer<S extends Iterable<any>>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<infer function (type parameter) AA> ? function (type parameter) AA
: never
/**
* Constructs an array type preserving non-emptiness.
*
* **Example** (Preserving non-emptiness)
*
* ```ts
* import type { Array } from "effect"
*
* type Result = Array.ReadonlyArray.With<readonly [number], string>
* // Result is NonEmptyArray<string>
* ```
*
* @category types
* @since 2.0.0
*/
export type type ReadonlyArray.With<S extends Iterable<any>, A> = S extends readonly [any, ...any[]] ? [A, ...A[]] : A[]Constructs an array type preserving non-emptiness.
Example (Preserving non-emptiness)
import type { Array } from "effect"
type Result = Array.ReadonlyArray.With<readonly [number], string>
// Result is NonEmptyArray<string>
With<function (type parameter) S in type ReadonlyArray.With<S extends Iterable<any>, A>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>, function (type parameter) A in type ReadonlyArray.With<S extends Iterable<any>, A>A> = function (type parameter) S in type ReadonlyArray.With<S extends Iterable<any>, A>S 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<any> ? 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) A in type ReadonlyArray.With<S extends Iterable<any>, A>A>
: interface Array<T>Array<function (type parameter) A in type ReadonlyArray.With<S extends Iterable<any>, A>A>
/**
* Creates a non-empty array if either input is non-empty.
*
* **Example** (Preserving non-emptiness from either input)
*
* ```ts
* import type { Array } from "effect"
*
* type Result = Array.ReadonlyArray.OrNonEmpty<
* readonly [number],
* ReadonlyArray<string>,
* number
* >
* // Result is NonEmptyArray<number>
* ```
*
* @category types
* @since 2.0.0
*/
export type type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A> = S extends readonly [any, ...any[]] ? [A, ...A[]] : T extends readonly [any, ...any[]] ? [A, ...A[]] : A[]Creates a non-empty array if either input is non-empty.
Example (Preserving non-emptiness from either input)
import type { Array } from "effect"
type Result = Array.ReadonlyArray.OrNonEmpty<
readonly [number],
ReadonlyArray<string>,
number
>
// Result is NonEmptyArray<number>
OrNonEmpty<
function (type parameter) S in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>,
function (type parameter) T in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>T extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>,
function (type parameter) A in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A
> = function (type parameter) S in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>S 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<any> ? 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) A in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A>
: function (type parameter) T in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>T 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<any> ? 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) A in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A>
: interface Array<T>Array<function (type parameter) A in type ReadonlyArray.OrNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A>
/**
* Creates a non-empty array only if both inputs are non-empty.
*
* **Example** (Preserving non-emptiness from both inputs)
*
* ```ts
* import type { Array } from "effect"
*
* type Result = Array.ReadonlyArray.AndNonEmpty<
* readonly [number],
* readonly [string],
* boolean
* >
* // Result is NonEmptyArray<boolean>
* ```
*
* @category types
* @since 2.0.0
*/
export type type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A> = S extends readonly [any, ...any[]] ? T extends readonly [any, ...any[]] ? [A, ...A[]] : A[] : A[]Creates a non-empty array only if both inputs are non-empty.
Example (Preserving non-emptiness from both inputs)
import type { Array } from "effect"
type Result = Array.ReadonlyArray.AndNonEmpty<
readonly [number],
readonly [string],
boolean
>
// Result is NonEmptyArray<boolean>
AndNonEmpty<
function (type parameter) S in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>S extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>,
function (type parameter) T in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>T extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>,
function (type parameter) A in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A
> = function (type parameter) S in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>S 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<any> ? function (type parameter) T in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>T 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<any> ? 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) A in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A>
: interface Array<T>Array<function (type parameter) A in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A>
: interface Array<T>Array<function (type parameter) A in type ReadonlyArray.AndNonEmpty<S extends Iterable<any>, T extends Iterable<any>, A>A>
/**
* Flattens a nested array type.
*
* **Example** (Flattening nested array types)
*
* ```ts
* import type { Array } from "effect"
*
* type Nested = ReadonlyArray<ReadonlyArray<number>>
* type Flattened = Array.ReadonlyArray.Flatten<Nested>
* // Flattened is Array<number>
* ```
*
* @category types
* @since 2.0.0
*/
export type type ReadonlyArray.Flatten<T extends ReadonlyArray<ReadonlyArray<any>>> = T extends readonly [readonly [any, ...any[]], ...(readonly [any, ...any[]])[]] ? [T[number][number], ...T[number][number][]] : T[number][number][]Flattens a nested array type.
Example (Flattening nested array types)
import type { Array } from "effect"
type Nested = ReadonlyArray<ReadonlyArray<number>>
type Flattened = Array.ReadonlyArray.Flatten<Nested>
// Flattened is Array<number>
Flatten<function (type parameter) T in type ReadonlyArray.Flatten<T extends ReadonlyArray<ReadonlyArray<any>>>T extends interface ReadonlyArray<T>ReadonlyArray<interface ReadonlyArray<T>ReadonlyArray<any>>> = function (type parameter) T in type ReadonlyArray.Flatten<T extends ReadonlyArray<ReadonlyArray<any>>>T 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<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<any>> ? 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) T in type ReadonlyArray.Flatten<T extends ReadonlyArray<ReadonlyArray<any>>>T[number][number]>
: interface Array<T>Array<function (type parameter) T in type ReadonlyArray.Flatten<T extends ReadonlyArray<ReadonlyArray<any>>>T[number][number]>
}