The minimum number of elements (inclusive). Must be a non-negative integer literal.
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)
Branded readonly array type for arrays with at least
MinLengthelements.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 onElm), so type-checking cost stays essentially independent of the size ofElmand of the bound. Prefer this type whenElmis a large type or the bound is large.For ergonomics, the structural part is not a plain
readonly Elm[]butMinLengthTuple<min(MinLength, 10), Elm>: indexed access belowmin(MinLength, 10)does not includeundefinedundernoUncheckedIndexedAccess. The prefix length is clamped at10so 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, thenMinLengthArray<M, Elm>is assignable toMinLengthArray<N, Elm>(an array of at leastMelements is also an array of at leastNelements). This is achieved by branding with a readonly tuple type that requires at leastMinLengthelements, which becomes a narrower type as the constraint gets stricter. The result is also covariant inElm.