Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It is possible (and popular) to have sum types in TypeScript by manually adding a tag, though.

  type Maybe<T> =
    | { kind: "Some", value: T }
    | { kind: "None" }


This is also why sum types are often referred to as tagged unions. They are a special case of a union, in Rust's case with support from the type system.


Yep, Typescript's control flow analysis, type literals, and union types allow us have tagged unions/sum types. Probably its best feature.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: