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



Wow did not know that. Looks quite confusing to me, but maybe I'm old or smth.

I expect those `"a" | "b" | "c"` to be expressed as an enum, not strings and |s.


There's almost no material benefit to expressing as an enum. You get type checking either way.


I got that. It's just that I'm not used to strings being some kind of enums.

But hey, why not!


Understandably so, it's the impressive power of TypeScript's type system. No other mainstream language has arbitrary union types like this.


Arbitrary unions of literals as types have been done before dynamically (Lisps, Prologs, Erlang) and done elsewhere statically(Python).

The more distinctive features in TypeScript would be what you can to with keyof/typeof, indexed access types, conditional types and especially mapped types.


TypeScript's unions are still impressive as you can union anything, not just literal types (TIL python has unions for literals, thanks).

Out of curiosity, what do you mean by dynamically? Isn't that just a case statement, or am I misunderstanding?


> TypeScript's unions are still impressive as you can union anything, not just literal types (TIL python has unions for literals, thanks).

You can do this in Python too..?




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

Search: