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!
head
tail
fromArray
sort
group

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 needed

Practice Exercises

1

Basic

Learn about basic in fp-ts nonemptyarray

beginnerStart
2

From Array

Learn about from array in fp-ts nonemptyarray

beginnerStart
3

Operations

Learn about operations in fp-ts nonemptyarray

beginnerStart
4

Sort

Learn about sort in fp-ts nonemptyarray

intermediateStart
5

Group

Learn about group in fp-ts nonemptyarray

intermediateStart
6

Practical

Learn about practical in fp-ts nonemptyarray

intermediateStart

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