Hacker News new | past | comments | ask | show | jobs | submit login
Collection of TypeScript Type Challenges (github.com/type-challenges)
24 points by nazwa on Sept 8, 2020 | hide | past | favorite | 6 comments



Anyone else found themselves going down the rabbit hole when doing TypeScript types, and spending more time on the types than the actual code?

They are insanely powerful but can be a little too addictive


Yes. It's been suggested that programmers are biased towards static type systems because they present interesting puzzles, and we like puzzles, even if they come at the expense of productivity. I don't want to agree with that, but I can't deny there's some truth to it.


Yeah. I learnt to use @ts-ignore but there are some religious people who judge quality of code by the coverage of how typed something is even if it is not necessarily benefiting.

Example: one or two requests to an isolated rest API with some data. While I get the point of documenting the properties if you don't use all of them and might need to in future, most of the time - it's not necessary. I have seen some people include a type generator for this.

or including complex generic mess. I myself am guilty of this one. A recent example comes to mind where I wanted to generate a multi dimensional array from an external api. Each entry in it would have specific first items and rest would be some complex typefoo.

I spent half an hour on writing the type. The thing is it's an isolated code and there is no one other than me working on it. Plus, there is visible code doing transformation of the data to turn it into specific entries. It's just kind of not necessary and the type isn't readable either.

[type<[...typeboo[][]]>, type<[...typemoo[][] ]>, ...typefoo[][]]

Something like that with imported types from another file.

I think it's easy to read types for someone coming into the project. I can hover over and see how it would look like if the case is not isolated.

Another thing that makes typescript type system alluring is that it's like an entirely different language due to javascript being so dynamic. It's similar to writing coq or some proof theorem language to verify the code. In this case, it's more data structures or data itself.

Maybe it's more similar to writing your own linter or subset of language by putting constraints through the type system [0]. I am not sure.

0] https://github.com/millsp/ts-toolbelt


I just found about and have been reaching for this type generator when I use rest APIs [0]. It’s worked great for the Twitter API so far. 10/10 would recommend. I started by scraping the API pages with script snippets that run on my own browser. Was wondering if something existed for JSON to TS and it looks like QuickType works pretty well. I kind of want to make something that will anonymize and compress the data for me from json and keep the particular types so I can keep feeding it through QuickType and keep around sample anonymized json data.

[0]: https://quicktype.io/


I find myself spending hours because they are too weak...

Not a Haskell type guy fanboy, but TS attempts to give you the tools to express yourself in types and falls significantly short.

Something like a single case discriminated union, proper discriminated unions, recursive types are all “basic”.


Can you be more elaborate about your last point like give examples where it's not proper?




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: