ts-type-forge
    Preparing search index...
    MinLengthArray: MinLengthTuple<ClampToPrefixCap<MinLength>, Elm> & TSTypeForgeInternals_BrandEncapsulated<
        Readonly<{ MinLength: MinLengthTuple<MinLength, 0> }>,
    >

    Branded readonly array type for arrays with at least MinLength elements.

    Unlike the structural tuple-based MinLengthTuple, the exact length constraint is encoded in the brand (a tuple of the literal 0, whose cost does not depend on Elm), so type-checking cost stays essentially independent of the size of Elm and of the bound. Prefer this type when Elm is a large type or the bound is large.

    For ergonomics, the structural part is not a plain readonly Elm[] but MinLengthTuple<min(MinLength, 10), Elm>: indexed access below min(MinLength, 10) does not include undefined under noUncheckedIndexedAccess. The prefix length is clamped at 10 so that the structural part stays cheap for large bounds; the clamp is monotone, so the subtyping relation below is unaffected.

    The brand is encoded so that the natural subtyping relation between length constraints is preserved: if M >= N, then MinLengthArray<M, Elm> is assignable to MinLengthArray<N, Elm> (an array of at least M elements is also an array of at least N elements). This is achieved by branding with a readonly tuple type that requires at least MinLength elements, which becomes a narrower type as the constraint gets stricter. The result is also covariant in Elm.

    Type Parameters

    • MinLength extends SupportedLength

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

    • Elm = unknown

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

    const isMinLengthArray = <N extends SupportedLength, E>(
    xs: readonly E[],
    minLength: N,
    ): xs is MinLengthArray<N, E> => xs.length >= minLength;

    const history = [0, 1, 2, 3] as unknown as MinLengthArray<3, number>;

    const nonEmpty: MinLengthArray<1, number> = history; // OK (3 >= 1)
    const first: number = history[0]; // OK — no `undefined` below min(N, 10)
    // const longer: MinLengthArray<5, number> = history; // Error! (3 < 5)