https://github.com/lumakernel/option-ts
https://github.com/lumakernel/option-ts
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lumakernel/option-ts
- Owner: LumaKernel
- Created: 2025-03-03T17:15:09.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-03-03T17:28:36.000Z (10 months ago)
- Last Synced: 2025-03-12T08:42:21.738Z (9 months ago)
- Language: TypeScript
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# option-ts
Rust-like interface Option class.
# Example
```ts
const v = Option.fromNullish(
await fetchSomething(), /* which returns { something: number } | null */
);
const maybeSomething = v.map((e) => e.something);
const unwrapped = maybeSomething.unwrapOr(Number.NaN);
```
# Is there .flatMap() method?
Use `.andThen()` instead.
# How to work on async works only when the value is some?
We may want to something like following intuitively in Rust.
```rust
match v {
Some(v) => {
v.work.await;
}
_ => {}
}
```
But yes, that couldn't be possible even if using `.match()`. Instead you can do
that as following.
```ts
await v.andThen(async (v) => await v.work()).awaited();
```
The method `.awaited()` converts from `Option>` into
`Promise>`.