News for hackers. Stuff that matters.
Re:An introduction to type programming in TypeScript by nvegater on Wednesday February 02, 2022 @05:21AM (#30175800)
I really like JS/TS Combo. My only critic is the Enums - particularly the String enums. It is really an absolute mess.
Re:An introduction to type programming in TypeScript by wruza on Wednesday February 02, 2022 @07:35AM (#30176651)
“Bare” enums are not better either. No introspection, bad debug-ability (you can’t print them in a human-readable way). Typescript enums are a good tradeoff between just string|string|… types and pure integers (or whatever type you assign to them). The former’s literals are indistinguishable from string literals at readsourcestime. The latter creates issues at debugtime.
Re:An introduction to type programming in TypeScript by epolanski on Wednesday February 02, 2022 @08:27AM (#30177170)
Union types are the way to go in 99% of cases you need enums.
Re:An introduction to type programming in TypeScript by _jumble on Wednesday February 02, 2022 @09:14AM (#30177791)
Yes, and generating those unions from a const array gives you even further flexibility with runtime access: `const myStrArray = ["a", "b", "c"] as const; type MyStrType = typeof myStrArray[number];`