Unwrapping
Prefer safe extractors
| Method | When empty | When present |
|---|---|---|
getOrElse(() => x) | returns x from thunk | returns value |
getOrReturn(x) | returns x | returns value |
getOrUndefined() | undefined | value |
get() | throws | value |
Use lazy getOrElse when the default is expensive:
opt.getOrElse(() => computeDefault());
Use getOrReturn for constants:
opt.getOrReturn(-1);
get() — use sparingly
Some('ok').get(); // 'ok'
None.get(); // throws Error: No such element
Only call get() when you have already checked isDefined, or immediately after a branch that narrows the type in your own code.
Interop patterns
With async-data:
import { Option } from '@ekz/option';
import type { AsyncData } from '@ekz/async-data';
function toUser(data: AsyncData<User>): Option<User> {
return data.toOption();
}
With nullable APIs:
const name = Option.of(apiResponse?.name).getOrReturn('Anonymous');
With arrays:
const first = Option.of(items[0]);
Anti-patterns
- Using
get()instead ofgetOrElse/getOrUndefined - Converting to
undefinedand back repeatedly — stay inOptionthrough the pipeline - Using
Option.ofwhen you meanSomeand the value must not be coerced from null