TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Functional Programming with TypeScript's Type System

90 点作者 evolveyourmind大约 2 年前

13 条评论

foepys大约 2 年前
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 未加载
Waterluvian大约 2 年前
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 未加载
nkrisc大约 2 年前
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 未加载
toastal大约 2 年前
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>
ralphc大约 2 年前
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.
bottlepalm大约 2 年前
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.
ck45大约 2 年前
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>
jcparkyn大约 2 年前
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.
rdez6173大约 2 年前
Fun exploration and exceptionally impractical. Love it.
coneonthefloor大约 2 年前
What a monstrosity.
firechickenbird大约 2 年前
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 未加载
hoelle大约 2 年前
Gross. I love it.
ekvintroj大约 2 年前
Try to type a flatMap and then we talk.
评论 #35590267 未加载
评论 #35590227 未加载
评论 #35590449 未加载