TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Functional Programming with TypeScript's Type System

90 pointsby evolveyourmindabout 2 years ago

13 comments

foepysabout 2 years ago
I don&#x27;t get TypeScript&#x27;s type system.<p>It is obviously very powerful and can model very complex type constraints.<p>But then you have stuff like this where it is not checking types as I would expect:<p><pre><code> interface Foo { bar: string; } const f = {bar: &quot;foobar&quot;} as Readonly&lt;Foo&gt;; function someFunc(): Foo { return f; &#x2F;&#x2F; No error or warning, even with all strict flags enabled }</code></pre>
评论 #35590325 未加载
评论 #35590574 未加载
评论 #35590905 未加载
评论 #35590597 未加载
评论 #35590217 未加载
评论 #35590264 未加载
Waterluvianabout 2 years ago
While no actual Turing machine’s tape is infinitely long, I found issues in TypeScript with how finite generics are.<p>You have to define every possible count of generic arguments if you want to preserve their types. And if you go above that count your type system degrades. I think there’s also a maximum of 7 or so before it doesn’t work. Beyond that and the generic type widens.<p>For example, Lodash enumerating types for 2 to 7 generic items per function: <a href="https:&#x2F;&#x2F;github.com&#x2F;DefinitelyTyped&#x2F;DefinitelyTyped&#x2F;blob&#x2F;045220a0682a1f437c08610db4ad6eaf76b2f9f0&#x2F;types&#x2F;lodash&#x2F;common&#x2F;array.d.ts#L142">https:&#x2F;&#x2F;github.com&#x2F;DefinitelyTyped&#x2F;DefinitelyTyped&#x2F;blob&#x2F;0452...</a><p>Admittedly I don’t understand the problem space well. I’ve just seen it happen to me and in others’ code. It might not actually be an issue, or is already fixed.
评论 #35589573 未加载
nkriscabout 2 years ago
As wonderfully absurd as this is, I learned more about TypeScript’s type system from this post than I have from its documentation.<p><i>Entirely</i> possible that PEBKAC, but I’ve found TypeScript’s documentation to be on the worse end of the programming language documentation quality spectrum.
评论 #35591000 未加载
评论 #35593276 未加载
toastalabout 2 years ago
Can you? Certainly. Should you? Ehhhhh…<p>The post is experimenting with the type system—which is neat—but before you think you should push this in production (having been in positions to work with heavy-FP’d code), consider an actual FP language if that’s the style you want. The ergonomics are so bad compared to any FP lang→JS option. Currying&#x2F;partial application, first-class composition&#x2F;bind&#x2F;apply, pattern matching not using a ._tag property with a String key, and more are just missing (see: migrating from PureScript&#x2F;Haskell[0] for fp-ts to see how verbose basic concepts becomes). The other issue is that with TypeScript being multiparadigm and idiomatic TS leaning to imperative&#x2F;OO due to ergonomics and Java’s legacy on culture&#x2F;education, there’s a good chance your coworkers&#x2F;contributors will be expecting a non-FP style which will cause even more friction as you try to justify ‘purity’ to folks that just see a verbose codebase that likely is leaning hard into a lib, quality as it is, like fp-ts which cosplays as Haskell, PureScript, OCaml, Idris, et. al.<p>[0]: <a href="https:&#x2F;&#x2F;gcanti.github.io&#x2F;fp-ts&#x2F;guides&#x2F;purescript.html" rel="nofollow">https:&#x2F;&#x2F;gcanti.github.io&#x2F;fp-ts&#x2F;guides&#x2F;purescript.html</a>
ralphcabout 2 years ago
Is it just me or do other people read things like &quot;covariance on a mutable generic type&quot; and just want to get stuff done? Maybe it&#x27;s because nowadays I do solo or small team projects but this is why I fled to Elixir, it&#x27;s mostly dynamically typed and you can gradually get into types when you want it, Elixir is cool with that.
bottlepalmabout 2 years ago
I’m really interested in this for fun, but it’s still way over my head. And this is someone who has been using typescript daily for years. Would love to read something more long form that builds up to this.
ck45about 2 years ago
Related: &quot;TypeScripting the technical interview&quot; <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35120084" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35120084</a>
jcparkynabout 2 years ago
I&#x27;ve done my share of weird stuff with TS types before, but I&#x27;d never seen the trick of using interfaces to make higher-order functions. Neat.
rdez6173about 2 years ago
Fun exploration and exceptionally impractical. Love it.
coneonthefloorabout 2 years ago
What a monstrosity.
firechickenbirdabout 2 years ago
At this point I&#x27;m wondering if the TypeScript type system can be used for dependant types that would allow formal verification of the programs
评论 #35594459 未加载
hoelleabout 2 years ago
Gross. I love it.
ekvintrojabout 2 years ago
Try to type a flatMap and then we talk.
评论 #35590267 未加载
评论 #35590227 未加载
评论 #35590449 未加载