你如何在 TypeScript 中模拟 ADT 和模式匹配? [英] How do you emulate ADTs and pattern matching in TypeScript?

查看:112
本文介绍了你如何在 TypeScript 中模拟 ADT 和模式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,从 0.9.5 开始,TypeScript(还)没有代数数据类型(联合类型)和模式匹配(用于解构它们).更重要的是,它甚至不支持接口上的 instanceof.您使用哪种模式来模拟具有最大类型安全性和最少样板代码的这些语言功能?

Unfortunately, as of 0.9.5, TypeScript doesn't (yet) have algebraic data types (union types) and pattern matching (to destructure them). What's more, it doesn't even support instanceof on interfaces. Which pattern do you use to emulate these language features with maximal type safety and minimal boilerplate code?

推荐答案

TypeScript 1.4 添加了 联合类型和类型保护.

TypeScript 1.4 adds union types and type guards.

这篇关于你如何在 TypeScript 中模拟 ADT 和模式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆