very huge typescript question
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
20 Replies
basically im trying to implement a result monad, it has two types,
Ok
and Err
and helper functions to work with it
in here i have a flatMap
function which maps a Result of Ok type. If the Result is of Err type, leaves it untouched. The map function must return a Resultmm yum so big and huge
yes
but the problem is that for some reason in the example i sent its inferring the Ok type as unknown
its working in the compiled js, just have type errors which im not sure where im going wrong
would love some help
it's gotta be this part, idk if you noticed it
it's tricky since
T
doesn't have anything constraining itbut ig u could say the same for errors aye
yeah it is
but like
idk what to change to fix it
cause errors will always be like that
and they are getting inferred correctly
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
that's only being returned for the
err
tho, not the ok
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fromNullable
just takes a value and turns it into a Result
of Ok
type and Err
if its either null or undefined
will look at the codesandbox now
so i guess the only easy way to do this is return a union of Ok
and Err
?
i def don't want unions because in the future i have to make a match
function which would be very hard to do on a union (i think)
actually now that i think about it it could workUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
match would take two functions, one would be the function that will run if the result is
Ok(x)
, the other one will run if the result is an Err
map_or_else
hehematch is sexier
yeah if you've just got results it's fine
im hating library ts atm
sucks to write
aint it fun
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah :(