Need help with typescript stuff
I am trying to implement the
Maybe
monad in typescript. It's like the Option
type in Rust.
Here's what I have so far:
Here the from
method is used to take in a value and convert it to a Maybe<value>
. For example:
But there's a problem, when I pass a value that could either be some type (like number, string, etc) or null or undefined, it returns Maybe<value | undefined>
. The Maybe monad handles this by using none
so there's no need to have Maybe<value | undefined>
, it just needs to be Maybe<value>
. For example:
How do I modify my code in order to implement this behaviour?
Playground link: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAcuApjAvDAygTwLYCMQA2AFAEQJiIkCUA3AFB2IAeADiAE6zAECGEEMALI9MuRAB4AKgD4YAbzowYoSFHYBXYFA5EW7AJYA3HlGTGC6xAC4YkmAB8YUTC0QgAZvCRV5AXwZK0Cb6wDAQINgSMkTmljaSVDbColGyCkpK+p4xPBbIKAUwYOoEBA6OsfmF6mAAJoju+hS1PuyIUOrsYEIiYgB0YEhS0kS0ARltHV1FiADuPSnDOXljSv6KYVDBoYMUS4kLYsPyG0qTnd0U88lH0eSIqzDrgVtQITDu7BFLlfEHN6kThkYFkYMtLKhCsVSuUYJVIWgavVGs1Wu0LodEAMhtExqcYOdpgC+uFIuCHvQ1uMWOpcAR3tgTMAABbiACqIzkYQiiAANEUkE8bFzSdYwb9bD4ULI2TQBRQbKNUDKngc2UCMqCiFBmfoIH14QU0Pc0VNLkhRvR8YTuqLtbr9ZVHs8YDS6QyeCx2SN3GBFRKEsqYGz-r0JByNZlsjq9QbchCjV4KKaMcTduGRnjgTbMSSeURffbY07ndTafTQu5eFBhF6OQW-eL42LA9LMd7Q4sI+lNdGHXG8gikw8CeiiWHsXt61mJmPuoWY47m6WNm6KzAAObtADy7AAogQIBnyTYQ-EHMHIyC+8Xm0OTaOzXDm1bs3OnP3KpSnnR1ipoDATCoLmnwREQADaACM-IAEz8gAzAAun0bAsKMYwAPQYcCAB6AD8QATS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
15 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
not super familiar with Omit
lemme look it up
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
is this how it would be used?
probably using it wrong cause this still can be undefined https://this-vegetable.is-from.space/Alacritty-v0.10.0-portable_(1)_xDQClClw7i.png
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
i fixed it i think?
just extending the param type to be either T or null or undefined fixed the type errors
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
this is interesting lol
i was trying to implement this https://nexxel-has.no-friends.xyz/brave_QxiJmzp095.png
but didn't need to do the assert part
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah me too