bcherny / bcherny/tsoption

Requiring a value to be Some

Open
#4 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
60
Forks
2
PR merge metrics
No merged PRs in 30d

Description

I have a function that returns an `Option`. I run this on several values and then process them further. Since I’m using `map` et al I still have an `Option` at the end of it. Now I want to require that some of the values be defined. I want to be able to say something like `option.orElse(() => throw new Error("this value must be defined")`, but `orElse` directly takes an `Option`. `flatMap` does the opposite of what I need, as it will only run the function when the value is `Some`.

It would be helpful to have something like [Rust’s `unwrap_or_else`](https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else) and [Scala’s `orElse`](https://www.scala-lang.org/api/current/scala/Option.html#orElse[B%3E:A](alternative:=%3EOption[B]):Option[B]) in the general case. In this specific case, I’ve been wondering if it might be better to define `get` on `Option` instead of on `Some` and have it throw an exception on `None` (cf. [Rust](https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap), [Scala](https://www.scala-lang.org/api/current/scala/Option.html#head:A), [F#](https://msdn.microsoft.com/visualfsharpdocs/conceptual/option.get%5b%27t%5d-function-%5bfsharp%5d)).

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the Option, Some, and None implementations, including the existing orElse and get entry points. Compare the requested unwrap-or-throw behavior with the current API and related Rust, Scala, and F# semantics. Done means an agreed API behavior is implemented and its edge cases are covered by the project’s tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.