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.

Show HN: Tagged Unions and Factories in TypeScript

2 pointsby nightlyherbabout 2 years ago
Wish sum types got more love in TypeScript.<p>I have settled on this style to minimize the boilerplate code needed to write tagged unions and tagged union factories.<p>Lines 1-8 are helper functions for declaring tagged union types. They are used for non-generic tagged unions.<p>Non-generic tagged unions&#x27; types should be inferred correctly.<p>Generic tagged unions are super hard to type, as TypeScript only allows functions to be typed as generic values.<p>Wish you enjoy this as well.

1 comment

cornelisabout 2 years ago
I really like the concept of tagged unions in TypeScript. I have difficulty understanding your code. Can you create a real life example?