Comments (4)
I've just found O.all from ts-belt 4.0.0.
const el1: O.Option<string> = O.Some("3");
const el2: O.Option<boolean> = O.Some(true);
const el3: O.Option<number> = O.Some(4);
const el4: O.Option<string[]> = O.Some(["hello"]);
const result =
pipe(
[el1,el2,el3,el4],
O.all,
O.map(([el1,el2,el3,el4])=> /* your function goes here */),
)
This is probably the right function for you.
from ts-belt.
I'd prefer ts-pattern
for these scenarios.
const el1: O.Option<string> = O.Some("3");
const el2: O.Option<boolean> = O.Some(true);
const el3: O.Option<number> = O.Some(4);
const el4: O.Option<string[]> = O.Some(["hello"]);
const optionsResult =
match([el1,el2,el3,el4])
.with([P.not(P.nullish),P.not(P.nullish),P.not(P.nullish),P.not(P.nullish)],
([el1,el2,el3,el4])=>({el1,el2,el3,el4}))
.otherwise(F.always(O.None))
I've experienced the same dx issue in the rescript. When handling >3 option types, using pattern matching was much better to read.
in other languages such as purescript or haskell, they have do-notation for this but unfortunately, javascript does not.
from ts-belt.
I am also a fan of ts-pattern.
I did as you described and it seems to be working well.
Thanks for your help, I appreciate it.
I thought of a utility function during my lunch break.
The following is not type safe as it uses as assertion.
const el1: O.Option<string> = O.Some("hello");
const el2: O.Option<boolean> = O.Some(true);
const el3: O.Option<number> = O.Some(4);
const el4: O.Option<string[]> = O.Some(["hello", "world"]);
type OptionObject<T> = {
[K in keyof T]: Option<T[K]>;
};
const zipAllOption = <T>(
options: OptionObject<T>,
): Option<{ [K in keyof T]: T[K] }> =>
pipe(
options,
D.toPairs,
A.every(([_, v]) => O.isSome(v)),
B.ifElse(
() => options as { [K in keyof T]: T[K] },
() => O.None,
),
);
const optionResult = zipAllOption({ el1, el2, el3, el4 });
from ts-belt.
Thanks!
from ts-belt.
Related Issues (20)
- Why an Option type instead of `type` | undefined? HOT 4
- Using unions for a result HOT 11
- ts-belt usage example flagged by TypeScript HOT 2
- why not `Do Notation` and `Applicative`
- Misleading annotations for Array.range
- Is this library still being maintained? HOT 1
- 🚧 Announcement: the current state of ts-belt 🚧 HOT 3
- `AR.isOk` and `AR.isError` not defined
- `AR.toOption` for `undefined` promise resolutions resulting in `{ BS_PRIVATE_NESTED_SOME_NONE: 0 }` HOT 1
- O.Option's functions with null literal value behaves differ from with undefined literal values HOT 1
- Bad Typing Ok / Error HOT 6
- A.dropWhile is completely broken HOT 1
- `A.take` does not return empty array when `n` greater than length of array (as documented) HOT 2
- foy for the building not working properly HOT 3
- Update benchmarks to latest package versions HOT 4
- Proposal: G.isFinite HOT 1
- suggestion: reboot with rescript core? HOT 3
- snippets, and linter
- Better S.startsWith
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-belt.