NonEmptyArray
Type-safe arrays with guaranteed at least one element
What is NonEmptyArray?
NonEmptyArray is an array that is guaranteed to have at least one element. This means operations like head, last, max, and min are always safe - they can never fail. Express this constraint in your types instead of checking at runtime.
// Instead of runtime checks: if (arr.length === 0) return undefined return arr[0] // Use NonEmptyArray: const first = NEA.head(arr) // always works!
Never Empty
Guaranteed at least one element
Type Safety
No need to check for empty arrays
Safe Operations
head, last, max, min always work
Better APIs
Express intent in types
NonEmptyArray in Action
❌ Runtime Checks
function getFirst(arr: number[]): number | undefined {
if (arr.length === 0) {
return undefined
}
return arr[0]
}
// Always need to check length✅ NonEmptyArray Approach
function getFirst(arr: NonEmptyArray<number>): number {
return NEA.head(arr)
}
// Guaranteed non-empty in types
// No runtime checks neededPractice Exercises
Basic
Learn about basic in fp-ts nonemptyarray
From Array
Learn about from array in fp-ts nonemptyarray
Operations
Learn about operations in fp-ts nonemptyarray
Sort
Learn about sort in fp-ts nonemptyarray
Group
Learn about group in fp-ts nonemptyarray
Practical
Learn about practical in fp-ts nonemptyarray
Why Learn NonEmptyArray?
Master NonEmptyArray
Learn the fundamental concepts and patterns that make NonEmptyArray powerful
6 Exercises
Practice with hands-on exercises from intermediate level
Production Ready
Apply NonEmptyArray patterns to build robust, type-safe applications