-
Notifications
You must be signed in to change notification settings - Fork 0
Option
ghaerdi edited this page Aug 14, 2026
·
2 revisions
Option<T> is a discriminated union — every value exposes a literal
__tag: "some" or "none" — so you can narrow with
if (opt.__tag === "some") (or isSome() / isNone()).
- Checking
- Extracting Values
- Mapping & Transformation
- Chaining & Side Effects
- Filtering
- Flattening & Transposing
- Inserting & Taking
- Pattern Matching
- Cloning
- Zipping
- Iteration
- Converting to Result
- Static Methods
-
isSome(): ReturnstrueifSome. -
isNone(): ReturnstrueifNone. -
isSomeAnd(fn): ReturnstrueifSomeand the value satisfiesfn. -
contains(value): ReturnstrueifSomeand the value equalsvalue.
-
unwrap(): Returns theSomevalue, throws ifNone. Use with caution. -
expect(message): Returns theSomevalue, throwsmessageifNone. -
unwrapOr(defaultValue): Returns theSomevalue ordefaultValueifNone. -
unwrapOrElse(fn): Returns theSomevalue or computes default usingfn()ifNone. -
unwrapOrDefault(): Returns theSomevalue or throws (noDefaulttrait in TypeScript).
-
map(fn): MapsSome<T>toSome<U>. LeavesNoneuntouched. -
mapOr(defaultValue, fn): AppliesfntoSomevalue, returnsdefaultValueifNone. -
mapOrElse(defaultFn, fn): AppliesfntoSomevalue, appliesdefaultFnifNone. -
mapOrDefault(defaultValue, fn): AppliesfntoSomevalue, returnsdefaultValueifNone.
-
and(res): ReturnsresifSome, else returnsNone. -
andThen(fn): Callsfn(someValue)ifSome, returns the resultingOption. -
or(res): Returns self ifSome, else returnsres. -
orElse(fn): Returns self ifSome, else callsfn()and returns the result. -
xor(other): ReturnsSomeif exactly one of self orotherisSome, elseNone. -
inspect(fn): Callsfn(someValue)ifSome, returns originalOption.
-
filter(predicate): ReturnsSome(value)ifSomeand predicate passes, elseNone.
-
flatten(): ConvertsOption<Option<T>>toOption<T>. -
transpose(): TransposesOption<Result<T, E>>intoResult<Option<T>, E>.
-
getOrInsert(value): Returns the contained value. IfNone, inserts and returnsvalue. -
getOrInsertWith(fn): Returns the contained value. IfNone, computes and insertsfn(). -
take(): Extracts the value, leaving the option asNone. Returns the value asSome. -
takeIf(predicate): Extracts the value ifSomeand predicate passes, leavingNone.
-
match(matcher): Executesmatcher.Some(value)ormatcher.None(), returning the result.
-
cloned(): Returns a newOptionwith a deep clone of theSomevalue (usingstructuredClone).
-
zip(other): ZipsSome(a)withSome(b)intoSome([a, b]), elseNone. -
zipWith(other, fn): ZipsSome(a)withSome(b)usingfn(a, b)intoSome(result), elseNone.
-
[Symbol.iterator](): Iterator protocol — yields theSomevalue if it is iterable.
-
okOr(err): ConvertsSome(v)toOk(v),NonetoErr(err). -
okOrElse(fn): ConvertsSome(v)toOk(v),NonetoErr(fn()).
-
Option.fromNullable(fn): Wraps a function that might returnnullorundefined. ReturnsSome(value)orNone. -
Option.isOption(value): Type guard, returnstrueifvalueisSomeorNone.