ts-type-forge
    Preparing search index...

    Type Alias BoundedLengthArray<MinLength, MaxLength, Elm>

    BoundedLengthArray: MaxLengthArray<MaxLength, Elm> & MinLengthArray<
        MinLength,
        Elm,
    >

    Branded readonly array type for arrays whose length is between MinLength and MaxLength elements (both inclusive). Defined as the intersection of MinLengthArray and MaxLengthArray, so both bounds can be weakened independently: BoundedLengthArray<M1, M2, Elm> is assignable to BoundedLengthArray<N1, N2, Elm> if M1 >= N1 and M2 <= N2.

    This is the brand-based, lightweight counterpart of the structural tuple-based BoundedLengthTuple; see MaxLengthArray for why it is much cheaper to type-check when the element type is large.

    Type Parameters

    • MinLength extends SupportedLength

      The minimum number of elements (inclusive). Must be a non-negative integer literal.

    • MaxLength extends SupportedLength

      The maximum number of elements (inclusive). Must be a non-negative integer literal.

    • Elm = unknown

      The type of elements in the array (defaults to unknown).

    const selection = [1, 2, 3] as unknown as BoundedLengthArray<1, 5, number>;

    const relaxed: BoundedLengthArray<0, 100, number> = selection; // OK ([1, 5] ⊆ [0, 100])
    const atLeast1: MinLengthArray<1, number> = selection; // OK
    const atMost5: MaxLengthArray<5, number> = selection; // OK
    // const strict: BoundedLengthArray<2, 5, number> = selection; // Error! (1 < 2)