TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Practical introduction to algebraic datatypes (ADTs) in TypeScript

71 pointsby elnygrenalmost 4 years ago

5 comments

lalaithionalmost 4 years ago
I think this article makes the &quot;sum&quot; and &quot;product&quot; terms out to be very complicated, when in fact it&#x27;s quite simple. If we have two enums<p><pre><code> enum Bool { True, False } enum Status { Waiting, Successful, Failed } </code></pre> We can combine them into a product type, like a tuple, or a sum type, like a union.<p><pre><code> type Product = (Bool, Status) type Sum = Bool | Status </code></pre> Now, we ask ourselves, what are the valid values of type Product?<p><pre><code> (True, Waiting) (True, Successful) (True, Failed) (False, Waiting) (False, Successful) (False, Failed) </code></pre> There are two Bool values, and three Status values, so there are 2×3=6 values for the product of Bool and Status. Now, what about the Sum type?<p><pre><code> True False Waiting Successful Failed </code></pre> We have 2+3=5 total values for the sum of Bool and Product.<p>Now, obviously, this math doesn&#x27;t quite work out for types with infinite values, like strings or bigints or arrays, but that&#x27;s where the analogy comes from.<p>If you extend this further, you can even figure out what an exponential type would be: the exponential type Bool^Status is a function that takes a Status as its argument and returns a Bool.
评论 #28143102 未加载
评论 #28144513 未加载
评论 #28171666 未加载
评论 #28145000 未加载
评论 #28151749 未加载
benrbrayalmost 4 years ago
Definitely take a look at fp-ts [1]! It&#x27;s mentioned at the end of the article, but I want to emphasize just how cool it is.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;fp-ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;fp-ts</a>
评论 #28142947 未加载
评论 #28142821 未加载
g_delgado14almost 4 years ago
I spoke about this with Feross Aboukhadijeh last week at SpeakeasyJS:<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=QyunIFfKLIU" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=QyunIFfKLIU</a>
insseinalmost 4 years ago
ADTs like the one described here for remote data is a good start, but I haven’t yet found a good way to compose multiple pieces of data that is in potentially different states.<p>We use another class based concept on top at work where you can compose multiple pieces of remote data together, but it really only works well for the happy path.
ramesh31almost 4 years ago
The switch case is still a bit redundant and inelegant, particularly considering how out of place switch case syntax feels in a Javascript codebase in general. Early returns are the way to go.
评论 #28146569 未加载
评论 #28145362 未加载
评论 #28146774 未加载