TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Tagged Unions and Factories in TypeScript

2 点作者 nightlyherb大约 2 年前
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

cornelis大约 2 年前
I really like the concept of tagged unions in TypeScript. I have difficulty understanding your code. Can you create a real life example?