typescript Feb 19, 2026
Non-Empty Arrays with Runtime Guard
Guarantee at least one element after a check.
A NonEmptyArray pairs well with a guard so your downstream code can rely on at least one item.
Type
non-empty-array.ts
type NonEmptyArray<T> = [T, ...T[]]
function assertNonEmpty<T>(value: T[]): asserts value is NonEmptyArray<T> {
if (value.length === 0) {
throw new Error('Expected at least one item')
}
}
const ids: string[] = []
assertNonEmpty(ids)
ids[0].toUpperCase()