Usage
import { isNonEmptyArray } from 'ngwr/utils';
if (isNonEmptyArray(rows)) {
// rows is [Row, ...Row[]] — `rows[0]` is non-nullable
highlight(rows[0]);
}Why ngwr provides this
With strict TS (noUncheckedIndexedAccess: true), if (arr.length > 0) arr[0] still types arr[0] as T | undefined — TS can't connect the length check to the index access. The guard narrows the tuple shape to [T, ...T[]] so [0] is T. Saves a non-null assertion.
// Native — with `noUncheckedIndexedAccess` enabled, length check
// doesn't propagate to the indexed access.
if (rows.length > 0) {
highlight(rows[0]);
// ^? Row | undefined ← still nullable!
}
// ngwr — narrows the tuple shape, so [0] is Row.
if (isNonEmptyArray(rows)) {
highlight(rows[0]);
// ^? Row ← clean
}API
| Name | Description | Type | Default |
|---|---|---|---|
isNonEmptyArray(value) | Asserts the array has at least one element; narrows the type to [T, ...T[]] so index 0 is non-nullable. Takes Maybe<T[]>, so a null / undefined list needs no separate check. | <T>(value: Maybe<T[]>) => value is [T, ...T[]] | — |