DEV Community

Cover image for Union Types with Javascript

Union Types with Javascript

Avalander on April 15, 2018

(Cover image: Lanterns, by Anna Sánchez Inglada. Original picture here) Lately I've been learning Elm and I'm entirely fascinated by its union typ...
Collapse
 
tgrecojs profile image
Thomas Greco

thank u for this! needed a refresher on union types w/ react & this was exactly what I was looking for! really appreciated the custom union helper function too. it was also super helpful in getting the juices flowing so that I could come to a solution for the task at hand.

declarative UIs FTW! thanks again :)

Collapse
 
avalander profile image
Avalander

Wonderful! I'm glad you found it useful!

Collapse
 
caiorcferreira profile image
Caio Ferreira

I'm adding this post on my always favorite list!
I had seen other implementations of algebric data types in Javascript and none was so simple and elegant.

Thank you for the incredible article and high fives for the solution!

Collapse
 
avalander profile image
Avalander

Thanks for your keen words, I appreciate it!

When I learned about algebraic data types I checked out a couple of Javascript libraries, but it seemed a bit overkill to use them, so I came up with this implementation.

Collapse
 
napicella profile image
Nicola Apicella

Hi! Nice article and great job with the examples. In the react example I think the code turned out to be slightly more readable with the union types than it would be with a bunch of ifs.
Anyway, it s a good tool to have in a coder s toolbox. Thanks!

Collapse
 
avalander profile image
Avalander

Thanks for your comments!

Collapse
 
rametta profile image
Jason

This is a really great article! If you haven't used Daggy JS before, I would highly recommend.

Collapse
 
avalander profile image
Avalander

Thank you! I had looked at Daggy, but I haven't used it in a real project yet.

Collapse
 
lluismf profile image
Lluís Josep Martínez

+1 just for citing the author of the photograph. Almost nobody does that.