2.5 KiB
2.5 KiB
name, description
| name | description |
|---|---|
| native-monad-option | Option<T> API reference. ALWAYS invoke when using Some, None, Option, OptionPromise, Option.from, Option.all, Option.partition, option.map, option.flatMap, option.match(2 args), option.filter(NO errorFactory). Do not guess Option methods — consult this skill. |
Option Usage Guide
Quick Reference
| Method | Signature (on Some) | Signature (on None) |
|---|---|---|
isSome() |
this is Some<T> |
this is never |
isNone() |
this is never |
this is None |
map(fn) |
(fn: (v: T) => U) => Some<U> |
() => None |
flatMap(fn) |
(fn: (v: T) => R) => R |
() => this |
match(onSome, onNone) |
2 callbacks | 2 callbacks |
filter(pred) |
NO errorFactory | () => None |
toNullable() |
T |
null |
toUndefined() |
T |
undefined |
toString() |
string |
string |
Critical Rules
match()takes exactly 2 callbacks:(onSome, onNone).filter()takes ONLY a predicate — NOerrorFactory(unlike Result).- Option has NO
mapErr()orflatMapErr()— those are Result/Query only. flatMapconstraint:R extends Option<any>— callback must return an Option.- Type guard: after
isSome(), access.value. None has no.value. none()returns a frozen singleton — do not try to constructnew None().Option.from()treats onlynull | undefinedas None.0,false,""becomeSome.
Anti-Patterns
// WRONG — Option.filter does NOT take errorFactory
option.filter((v) => v > 0, () => new Error("bad"));
// CORRECT — just a predicate
option.filter((v) => v > 0);
// WRONG — mapErr does not exist on Option
option.mapErr((e) => new Error(e));
// CORRECT — Option has no error channel. Use Result if you need errors.
// WRONG — fromNullable does not exist
Option.fromNullable(value);
// CORRECT
Option.from(value);
Namespace & Async Methods
Read references/api-surface.md for the complete Option namespace (.from, .all, .partition, .compact, .some,
.every) and OptionPromise namespace.