Option
Option Combinators
Working with multiple Options and converting between types.
Overview
| Function | Purpose | Signature |
|---|---|---|
collect | All Some → Some(array), any None → None | Option<T>[] → Option<T[]> |
partition | Separate Some values and None count | Option<T>[] → [T[], number] |
toResult | Convert to Result with error | Option<T> → Result<T, E> |
toNullable | Convert to nullable | Option<T> → T | null |
collect()
All-or-nothing: returns Some(array) only if all Options are Some.
collect<T>(options: readonly Option<T>[]): Option<T[]>Usage
collect([some(1), some(2), some(3)])
// => Some([1, 2, 3])
collect([some(1), none(), some(3)])
// => NoneReal-World: Form Validation
const validateForm = (data: SignupForm) => {
const fields = collect([
validateEmail(data.email),
validatePassword(data.password),
validateAge(data.age)
])
return map(fields, ([email, password, age]) => ({
email,
password,
age
}))
}
// Returns Some(validData) only if all fields valid
// Returns None if any field invalidpartition()
Separate Some values from Nones.
partition<T>(options: readonly Option<T>[]): [T[], number]Usage
const [values, noneCount] = partition([
some(1),
none(),
some(2),
none(),
some(3)
])
// values: [1, 2, 3]
// noneCount: 2Real-World: Batch Operations
const processUsers = async (ids: string[]) => {
const results = await Promise.all(ids.map(fetchUser))
const [users, failedCount] = partition(results)
console.log(`Processed: ${users.length}, Failed: ${failedCount}`)
return users
}toResult()
Convert Option to Result, providing error for None case.
toResult<T, E>(option: Option<T>, error: E): Result<T, E>Usage
toResult(some(42), 'error')
// => Ok(42)
toResult(none(), 'No value')
// => Err('No value')Real-World: API Error Responses
const getUser = (id: string): Result<User, ApiError> =>
R.pipe(
findUserById(id),
toResult({ code: 'NOT_FOUND', message: `User ${id} not found` })
)toNullable()
Convert Option back to nullable type.
toNullable<T>(option: Option<T>): T | nullUsage
toNullable(some(42)) // => 42
toNullable(none()) // => nullReal-World: External API Payloads
const apiPayload = {
userId: toNullable(maybeUserId),
email: toNullable(maybeEmail),
metadata: toNullable(maybeMetadata)
}
// Converts Options to null for JSON serializationNext Steps
- Patterns - Real-world combinator recipes
- API Reference - Complete signatures